8000 [LEET-0000] clean up · vaisham92/Leetcode@5f2480a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f2480a

Browse files
[LEET-0000] clean up
1 parent 09f2937 commit 5f2480a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeVerticalOrderTraversal.java)| O(n)|O(n) | Medium| HashMap, BFS
7373
|311|[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SparseMatrixMultiplication.java)| O(m*n*l)|O(m*l)| Medium|
7474
|308|[Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RangeSumQuery2DMutable.java)| ? | ? | Hard| Tree
75+
|306|[Additive Number](https://leetcode.com/problems/additive-number/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/AdditiveNumber.java)| ? | ? | Medium|
7576
|305|[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberofIslandsII.java)| ? | ? | Hard| Union Find
7677
|302|[Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SmallestRectangleEnclosingBlackPixels.java)| ? | O(m*n) | Hard| DFS, BFS
7778
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RemoveInvalidParentheses.java)| ? | ? | Hard| BFS
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Additive number is a string whose digits can form additive sequence.
5+
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.
7+
8+
For example:
9+
"112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
10+
11+
1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
12+
"199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199.
13+
1 + 99 = 100, 99 + 100 = 199
14+
Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.
15+
16+
Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.
17+
18+
Follow up:
19+
How would you handle overflow for very large input integers?
20+
*/
21+
public class AdditiveNumber {
22+
23+
public boolean isAdditiveNumber(String num) {
24+
if (num == null || num.length() < 3) {
25+
return false;
26+
}
27+
int len = num.length();
28+
for (int i = 1; i < len; i++) {
29+
for (int j = i + 1; j < len; j++) {
30+
int first = 0, second = i, third = j;
31+
if (num.charAt(second) == '0' && third > second + 1){
32+
break;// This is the pruning part, for each iteration, if this condition
33+
// meets, returns false immediately
34+
}
35+
while (third < len) {
36+
Long result = Long.parseLong(num.substring(first, second))
37+
+ Long.parseLong(num.substring(second, third));
38+
if (num.substring(third).startsWith((result.toString()))) {
39+
first = second;
40+
second = third;
41+
third += result.toString().length();
42+
} else {
43+
break;// This is another part of pruning! Cool!
44+
}
45+
}
46+
if(third == len){
47+
return true;
48+
}
49+
}
50+
}
51+
return false;
52+
}
53+
54+
}

0 commit comments

Comments
 (0)
0