|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 306. Additive Number |
| 5 | + * |
4 | 6 | * Additive number is a string whose digits can form additive sequence.
|
5 | 7 |
|
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. |
7 | 10 |
|
8 | 11 | For example:
|
9 | 12 | "112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
|
|
19 | 22 | How would you handle overflow for very large input integers?
|
20 | 23 | */
|
21 | 24 | 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 | + } |
29 | 34 | }
|
30 | 35 | }
|
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) { |
40 | 36 | return false;
|
41 | 37 | }
|
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) { |
50 | 44 | return false;
|
51 | 45 | }
|
| 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; |
52 | 58 | }
|
53 |
| - return true; |
54 | 59 | }
|
55 | 60 |
|
56 | 61 | }
|
0 commit comments