|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.utils.CommonUtils; |
4 |
| - |
5 |
| -/**66. Plus One |
6 |
| -Given a non-negative number represented as an array of digits, plus one to the number. |
7 |
| -
|
8 |
| -The digits are stored such that the most significant digit is at the head of the list.*/ |
| 3 | +/** |
| 4 | + * 66. Plus One |
| 5 | + * |
| 6 | + * Given a non-negative number represented as an array of digits, plus one to the number. The digits |
| 7 | + * are stored such that the most significant digit is at the head of the list. |
| 8 | + */ |
9 | 9 | public class _66 {
|
10 |
| - //also looked at Discuss, basically the same idea as mine, just their code is more concise. |
11 | 10 |
|
| 11 | + public static class Solution1 { |
12 | 12 | public int[] plusOne(int[] digits) {
|
13 |
| - boolean carry = false; |
14 |
| - int len = digits.length; |
15 |
| - int[] temp = digits; |
16 |
| - //process the last digit at first, to get carry value |
17 |
| - if (digits[len - 1] + 1 == 10) { |
18 |
| - carry = true; |
19 |
| - temp[len - 1] = 0; |
20 |
| - } else { |
21 |
| - temp[len - 1] += 1; |
22 |
| - return temp; |
23 |
| - } |
24 |
| - |
25 |
| - //start from the second last element |
26 |
| - for (int i = len - 2; i >= 0; i--) { |
27 |
| - if (carry && temp[i] + 1 == 10) { |
28 |
| - temp[i] = 0; |
29 |
| - carry = true; |
30 |
| - } else if (carry) { |
31 |
| - temp[i] += 1; |
32 |
| - carry = false; |
33 |
| - } |
34 |
| - } |
35 |
| - if (carry && temp[0] == 0) { |
36 |
| - int[] res = new int[len + 1]; |
37 |
| - res[0] = 1; |
38 |
| - //all the rest of the numbers should all be zeroes, so we don't need to copy from the original array |
39 |
| - return res; |
40 |
| - } |
| 13 | + boolean carry = false; |
| 14 | + int len = digits.length; |
| 15 | + int[] temp = digits; |
| 16 | + //process the last digit at first, to get carry value |
| 17 | + if (digits[len - 1] + 1 == 10) { |
| 18 | + carry = true; |
| 19 | + temp[len - 1] = 0; |
| 20 | + } else { |
| 21 | + temp[len - 1] += 1; |
41 | 22 | return temp;
|
42 |
| - } |
| 23 | + } |
43 | 24 |
|
44 |
| - public static void main(String... strings) { |
45 |
| - _66 test = new _66(); |
46 |
| -// int[] digits = new int[]{9,9,9,9}; |
47 |
| -// int[] digits = new int[]{8,9,9,9}; |
48 |
| - int[] digits = new int[]{2, 4, 9, 3, 9}; |
49 |
| - int[] res = test.plusOne(digits); |
50 |
| - CommonUtils.printArray(res); |
| 25 | + //start from the second last element |
| 26 | + for (int i = len - 2; i >= 0; i--) { |
| 27 | + if (carry && temp[i] + 1 == 10) { |
| 28 | + temp[i] = 0; |
| 29 | + carry = true; |
| 30 | + } else if (carry) { |
| 31 | + temp[i] += 1; |
| 32 | + carry = false; |
| 33 | + } |
| 34 | + } |
| 35 | + if (carry && temp[0] == 0) { |
| 36 | + int[] res = new int[len + 1]; |
| 37 | + res[0] = 1; |
| 38 | + //all the rest of the numbers should all be zeroes, so we don't need to copy from the original array |
| 39 | + return res; |
| 40 | + } |
| 41 | + return temp; |
51 | 42 | }
|
| 43 | + } |
52 | 44 | }
|
0 commit comments