|
| 1 | +/** |
| 2 | + * [306] Additive Number |
| 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 | + * Given a string containing only digits '0'-'9', write a function to determine if it's an additive number. |
| 9 | + * |
| 10 | + * Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * |
| 14 | + * |
| 15 | + * Input: "112358" |
| 16 | + * Output: true |
| 17 | + * Explanation: The digits can form an additive sequence: 1, 1, 2, 3, 5, 8. |
| 18 | + * 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8 |
| 19 | + * |
| 20 | + * |
| 21 | + * Example 2: |
| 22 | + * |
| 23 | + * |
| 24 | + * Input: "199100199" |
| 25 | + * Output: true |
| 26 | + * Explanation: The additive sequence is: 1, 99, 100, 199<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">.</span> |
| 27 | + * 1 + 99 = 100, 99 + 100 = 199 |
| 28 | + * |
| 29 | + * Follow up:<br /> |
| 30 | + * How would you handle overflow for very large input integers? |
| 31 | + */ |
| 32 | +pub struct Solution {} |
| 33 | + |
| 34 | +// submission codes start here |
| 35 | + |
| 36 | +// first_cut second_cut third_cut |
| 37 | +// V V V |
| 38 | +// 1 99 100 199 |
| 39 | +impl Solution { |
| 40 | + pub fn is_additive_number(num: String) -> bool { |
| 41 | + let mut chs: Vec<u32> = num.chars().map(|c| c.to_digit(10).unwrap() ).collect(); |
| 42 | + let mut num1 = 0; |
| 43 | + let len = chs.len(); |
| 44 | + // first cut |
| 45 | + for i in 0..(len / 2 + 1) { |
| 46 | + num1 = num1 * 10 + chs[i]; |
| 47 | + if Solution::second_cut(i+1, len, num1, &chs) { |
| 48 | + return true |
| 49 | + } |
| 50 | + if num1 == 0 { |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + false |
| 55 | + } |
| 56 | + |
| 57 | + fn second_cut(from: usize, len: usize, num1: u32, chs: &Vec<u32>) -> bool { |
| 58 | + let mut num2 = 0; |
| 59 | + for i in from..len { |
| 60 | + num2 = num2 * 10 + chs[i]; |
| 61 | + if Solution::third_cut(i+1, len, num1, num2, chs, false) { |
| 62 | + return true |
| 63 | + } |
| 64 | + if num2 == 0 { |
| 65 | + break |
| 66 | + } |
| 67 | + } |
| 68 | + false |
| 69 | + } |
| 70 | + |
| 71 | + fn third_cut(from: usize, len: usize, num1: u32, num2: u32, chs: &Vec<u32>, found: bool) -> bool { |
| 72 | + if found && from >= len { |
| 73 | + return true |
| 74 | + } |
| 75 | + let mut num3 = 0; |
| 76 | + for i in from..len { |
| 77 | + num3 = num3 * 10 + chs[i]; |
| 78 | + if num3 == num2 + num1 { |
| 79 | + if Solution::third_cut(i+1, len, num2, num3, chs, true) { |
| 80 | + return true |
| 81 | + } |
| 82 | + } else if num3 == 0 || num3 > num1 + num2 { |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + false |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// submission codes end |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::*; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_306() { |
| 98 | + assert_eq!(Solution::is_additive_number("112358".to_owned()), true); |
| 99 | + assert_eq!(Solution::is_additive_number("199100199".to_owned()), true); |
| 100 | + assert_eq!(Solution::is_additive_number("1991001990".to_owned()), false); |
| 101 | + assert_eq!(Solution::is_additive_number("1023".to_owned()), false); |
| 102 | + } |
| 103 | +} |
0 commit comments