306. Additive Number Medium

@problem@discussion
#String#Backtracking



1/**
2 * [306] Additive Number
3 *
4 * An additive number is a string whose digits can form an additive sequence.
5 * 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.
6 * Given a string containing only digits, return true if it is an additive number or false otherwise.
7 * Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.
8 *  
9 * Example 1:
10 * 
11 * Input: "112358"
12 * Output: true
13 * Explanation: 
14 * The digits can form an additive sequence: 1, 1, 2, 3, 5, 8. 
15 * 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
16 * 
17 * Example 2:
18 * 
19 * Input: "199100199"
20 * Output: true
21 * Explanation: 
22 * The additive sequence is: 1, 99, 100, 199. 
23 * 1 + 99 = 100, 99 + 100 = 199
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	1 <= num.length <= 35
29 * 	num consists only of digits.
30 * 
31 *  
32 * Follow up: How would you handle overflow for very large input integers?
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/additive-number/
38// discuss: https://leetcode.com/problems/additive-number/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn is_additive_number(num: String) -> bool {
44        false
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_306() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.