Skip to content

Commit a023ef6

Browse files
refactor 66
1 parent 83bc453 commit a023ef6

File tree

2 files changed

+70
-43
lines changed

2 files changed

+70
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,44 @@
11
package com.fishercoder.solutions;
22

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+
*/
99
public class _66 {
10-
//also looked at Discuss, basically the same idea as mine, just their code is more concise.
1110

11+
public static class Solution1 {
1212
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;
4122
return temp;
42-
}
23+
}
4324

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;
5142
}
43+
}
5244
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._66;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _66Test {
10+
private static _66.Solution1 solution1;
11+
private static int[] digits;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _66.Solution1();
16+
}
17+
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+
}
23+
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+
}
29+
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+
}
35+
}

0 commit comments

Comments
 (0)