Skip to content

Commit 46815c1

Browse files
authored
add _66.java solution2 (fishercoder1534#116)
* add _66.java solution2 * write new tests for solution2 * remove import
1 parent 748082e commit 46815c1

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,21 @@ public int[] plusOne(int[] digits) {
2020
return newNumber;
2121
}
2222
}
23+
24+
public static class Solution2 {
25+
public int[] plusOne(int[] digits) {
26+
int len = digits.length;
27+
for (int i = len - 1; i >= 0; i--) {
28+
if (digits[i] == 9) {
29+
digits[i] = 0;
30+
} else {
31+
digits[i]++;
32+
return digits;
33+
}
34+
}
35+
int[] newNumber = new int[len + 1];
36+
newNumber[0] = 1;
37+
return newNumber;
38+
}
39+
}
2340
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.fishercoder;
22

3-
import com.fishercoder.solutions._66;
3+
import static org.junit.Assert.assertArrayEquals;
4+
45
import org.junit.BeforeClass;
56
import org.junit.Test;
67

7-
import static org.junit.Assert.assertArrayEquals;
8+
import com.fishercoder.solutions._66;
89

910
public class _66Test {
1011
private static _66.Solution1 solution1;
12+
private static _66.Solution2 solution2;
1113
private static int[] digits;
1214

1315
@BeforeClass
1416
public static void setup() {
1517
solution1 = new _66.Solution1();
18+
solution2 = new _66.Solution2();
1619
}
1720

1821
@Test
@@ -32,4 +35,23 @@ public void test3() {
3235
digits = new int[]{2, 4, 9, 3, 9};
3336
assertArrayEquals(new int[]{2, 4, 9, 4, 0}, solution1.plusOne(digits));
3437
}
38+
39+
@Test
40+
public void test4() {
41+
digits = new int[]{9, 9, 9, 9, 9};
42+
assertArrayEquals(new int[]{1, 0, 0, 0, 0, 0}, solution2.plusOne(digits));
43+
}
44+
45+
@Test
46+
public void test5() {
47+
digits = new int[]{8, 9, 9, 9, 9};
48+
assertArrayEquals(new int[]{9, 0, 0, 0, 0}, solution2.plusOne(digits));
49+
}
50+
51+
@Test
52+
public void test6() {
53+
digits = new int[]{2, 4, 9, 4, 9};
54+
assertArrayEquals(new int[]{2, 4, 9, 5, 0}, solution2.plusOne(digits));
55+
}
56+
3557
}

0 commit comments

Comments
 (0)