Skip to content

Commit 22b6571

Browse files
refactor 306
1 parent 73d113a commit 22b6571

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed
Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 306. Additive Number
5+
*
46
* Additive number is a string whose digits can form additive sequence.
57
6-
A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.
8+
A valid additive sequence should contain at least three numbers.
9+
Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.
710
811
For example:
912
"112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
@@ -19,38 +22,40 @@
1922
How would you handle overflow for very large input integers?
2023
*/
2124
public class _306 {
22-
/**Credit: https://discuss.leetcode.com/topic/29856/java-recursive-and-iterative-solutions/2*/
23-
public boolean isAdditiveNumber(String num) {
24-
int n = num.length();
25-
for (int i = 1; i <= n / 2; ++i) {
26-
for (int j = 1; Math.max(j, i) <= n - i - j; ++j) {
27-
if (isValid(i, j, num)) {
28-
return true;
25+
public static class Solution1 {
26+
/** Credit: https://discuss.leetcode.com/topic/29856/java-recursive-and-iterative-solutions/2 */
27+
public boolean isAdditiveNumber(String num) {
28+
int n = num.length();
29+
for (int i = 1; i <= n / 2; ++i) {
30+
for (int j = 1; Math.max(j, i) <= n - i - j; ++j) {
31+
if (isValid(i, j, num)) {
32+
return true;
33+
}
2934
}
3035
}
31-
}
32-
return false;
33-
}
34-
35-
private boolean isValid(int i, int j, String num) {
36-
if (num.charAt(0) == '0' && i > 1) {
37-
return false;
38-
}
39-
if (num.charAt(i) == '0' && j > 1) {
4036
return false;
4137
}
42-
String sum;
43-
Long x1 = Long.parseLong(num.substring(0, i));
44-
Long x2 = Long.parseLong(num.substring(i, i + j));
45-
for (int start = i + j; start != num.length(); start += sum.length()) {
46-
x2 = x2 + x1;
47-
x1 = x2 - x1;
48-
sum = x2.toString();
49-
if (!num.startsWith(sum, start)) {
38+
39+
private boolean isValid(int i, int j, String num) {
40+
if (num.charAt(0) == '0' && i > 1) {
41+
return false;
42+
}
43+
if (num.charAt(i) == '0' && j > 1) {
5044
return false;
5145
}
46+
String sum;
47+
Long x1 = Long.parseLong(num.substring(0, i));
48+
Long x2 = Long.parseLong(num.substring(i, i + j));
49+
for (int start = i + j; start != num.length(); start += sum.length()) {
50+
x2 = x2 + x1;
51+
x1 = x2 - x1;
52+
sum = x2.toString();
53+
if (!num.startsWith(sum, start)) {
54+
return false;
55+
}
56+
}
57+
return true;
5258
}
53-
return true;
5459
}
5560

5661
}

src/test/java/com/fishercoder/_306Test.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/27/17.
11-
*/
129
public class _306Test {
13-
private static _306 test;
14-
private static String num;
10+
private static _306.Solution1 solution1;
11+
private static String num;
1512

16-
@BeforeClass
17-
public static void setup() {
18-
test = new _306();
19-
}
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _306.Solution1();
16+
}
2017

21-
@Test
22-
public void test1() {
23-
num = "0235813";
24-
assertEquals(false, test.isAdditiveNumber(num));
25-
}
18+
@Test
19+
public void test1() {
20+
num = "0235813";
21+
assertEquals(false, solution1.isAdditiveNumber(num));
22+
}
2623

27-
@Test
28-
public void test2() {
29-
num = "000";
30-
assertEquals(true, test.isAdditiveNumber(num));
31-
}
24+
@Test
25+
public void test2() {
26+
num = "000";
27+
assertEquals(true, solution1.isAdditiveNumber(num));
28+
}
3229

33-
@Test
34-
public void test3() {
35-
num = "011235";
36-
assertEquals(true, test.isAdditiveNumber(num));
37-
}
30+
@Test
31+
public void test3() {
32+
num = "011235";
33+
assertEquals(true, solution1.isAdditiveNumber(num));
34+
}
3835
}

0 commit comments

Comments
 (0)