Skip to content

Commit b719ec8

Browse files
refactor 66
1 parent 492de8c commit b719ec8

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

src/main/java/com/fishercoder/solutions/_66.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,38 @@
33
/**
44
* 66. Plus One
55
*
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.
6+
* Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
7+
* The digits are stored such that the most significant digit is at the head of the list,
8+
* and each element in the array contain a single digit.
9+
*
10+
* You may assume the integer does not contain any leading zero, except the number 0 itself.
11+
*
12+
* Example 1:
13+
* Input: [1,2,3]
14+
* Output: [1,2,4]
15+
* Explanation: The array represents the integer 123.
16+
*
17+
* Example 2:
18+
* Input: [4,3,2,1]
19+
* Output: [4,3,2,2]
20+
* Explanation: The array represents the integer 4321.
821
*/
922
public class _66 {
1023

1124
public static class Solution1 {
25+
/**credit: https://leetcode.com/problems/plus-one/discuss/24082/My-Simple-Java-Solution*/
1226
public int[] plusOne(int[] digits) {
1327
int len = digits.length;
14-
int[] temp = digits;
15-
1628
for (int i = len - 1; i >= 0; i--) {
17-
if (temp[i] + 1 == 10) {
18-
temp[i] = 0;
19-
} else {
20-
temp[i] += 1;
21-
return temp;
29+
if (digits[i] < 9) {
30+
digits[i]++;
31+
return digits;
2232
}
33+
digits[i] = 0;
2334
}
24-
if (temp[0] == 0) {
25-
int[] res = new int[len + 1];
26-
res[0] = 1; //all the rest of the numbers should all be zeroes, so we don't need to copy from the original array
27-
return res;
28-
} else {
29-
return temp;
30-
}
35+
int[] newNumber = new int[len + 1];
36+
newNumber[0] = 1;
37+
return newNumber;
3138
}
3239
}
3340
}

src/test/java/com/fishercoder/_66Test.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
import static org.junit.Assert.assertArrayEquals;
88

99
public class _66Test {
10-
private static _66.Solution1 solution1;
11-
private static int[] digits;
10+
private static _66.Solution1 solution1;
11+
private static int[] digits;
1212

13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _66.Solution1();
16-
}
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _66.Solution1();
16+
}
1717

18-
@Test
19-
public void test1() {
20-
digits = new int[] {9, 9, 9, 9};
21-
assertArrayEquals(new int[] {1, 0, 0, 0, 0}, solution1.plusOne(digits));
22-
}
18+
@Test
19+
public void test1() {
20+
digits = new int[]{9, 9, 9, 9};
21+
assertArrayEquals(new int[]{1, 0, 0, 0, 0}, solution1.plusOne(digits));
22+
}
2323

24-
@Test
25-
public void test2() {
26-
digits = new int[] {8, 9, 9, 9};
27-
assertArrayEquals(new int[] {9, 0, 0, 0}, solution1.plusOne(digits));
28-
}
24+
@Test
25+
public void test2() {
26+
digits = new int[]{8, 9, 9, 9};
27+
assertArrayEquals(new int[]{9, 0, 0, 0}, solution1.plusOne(digits));
28+
}
2929

30-
@Test
31-
public void test3() {
32-
digits = new int[] {2, 4, 9, 3, 9};
33-
assertArrayEquals(new int[] {2, 4, 9, 4, 0}, solution1.plusOne(digits));
34-
}
30+
@Test
31+
public void test3() {
32+
digits = new int[]{2, 4, 9, 3, 9};
33+
assertArrayEquals(new int[]{2, 4, 9, 4, 0}, solution1.plusOne(digits));
34+
}
3535
}

0 commit comments

Comments
 (0)