[leetcode] 306. Additive Number 解题报告
2016-05-10 01:09
375 查看
题目链接: https://leetcode.com/problems/additive-number/
Additive number is a string whose digits can form additive sequence.
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.
For example:
also an additive number, the additive sequence is:
Note: Numbers in the additive sequence cannot have leading zeros, so sequence
Given a string containing only digits
additive number.
Follow up:
How would you handle overflow for very large input integers?
思路: 一个基本的思想就是确定了第一个和第二个数之后, 以后的数就是验证了, 所以只要枚举第一和第二个数, 然后不断验证下面的字符串子串是否是前两个数的和即可. 因为数字可能超出整数的范围, 因此在验证一个数是否是另外两个和的时候, 可以用字符串相加来模拟整数相加. 另外需要注意的是'0'字符, 如果他在子串的首位, 那么他只能以"0"的形式存在.
代码如下:
Additive number is a string whose digits can form additive sequence.
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.
For example:
"112358"is an additive number because the digits can form an additive sequence:
1, 1, 2, 3, 5, 8.
1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
"199100199"is
also an additive number, the additive sequence is:
1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199
Note: Numbers in the additive sequence cannot have leading zeros, so sequence
1, 2, 03or
1, 02, 3is invalid.
Given a string containing only digits
'0'-'9', write a function to determine if it's an
additive number.
Follow up:
How would you handle overflow for very large input integers?
思路: 一个基本的思想就是确定了第一个和第二个数之后, 以后的数就是验证了, 所以只要枚举第一和第二个数, 然后不断验证下面的字符串子串是否是前两个数的和即可. 因为数字可能超出整数的范围, 因此在验证一个数是否是另外两个和的时候, 可以用字符串相加来模拟整数相加. 另外需要注意的是'0'字符, 如果他在子串的首位, 那么他只能以"0"的形式存在.
代码如下:
class Solution { public: string getSum(string num1, string num2) { int val, flag = 0, len1 = num1.size(), len2 = num2.size(); string sum; while(len1 || len2) { int val = 0; if(len1 > 0) val += num1[len1 -1] - '0', len1--; if(len2 > 0) val += num2[len2 -1] - '0', len2--; sum += to_string((val+flag)%10); flag = (val+flag)/10; } if(flag) sum += "1"; reverse(sum.begin(), sum.end()); return sum; } bool DFS(string& num1, string& num2, string& num3) { if(num3.size() ==0) return true; string sum = getSum(num1, num2); for(int i = 1; i <= num3.size(); i++) { string str = num3.substr(0, i), tem = num3.substr(i); if(str==sum && DFS(num2, str, tem)) return true; if(num3[0] == '0') break; } return false; } bool isAdditiveNumber(string num) { if(num.size() < 3) return false; int len = num.size(); for(int i = 1; i <= len-2; i++) { string num1=num.substr(0, i); for(int j = i+1; j <= len-1; j++ ) { string num2 =num.substr(i, j-i), num3=num.substr(j); if(DFS(num1, num2, num3)) return true; if(num[i] == '0') break; } if(num[0] == '0') break; } return false; } };
相关文章推荐
- Python再接触
- 计算机网络之万维网WWW
- Hibernate关系映射(注解)
- Unity Input Touch fingerId
- C++作业5
- 02 C#(int)中Convert、Parse、TryParse的区别
- Open-E DSS V7 应用系列之一 系统简介
- Open-E DSS V7 应用系列之一 系统简介
- Open-E DSS V7 应用系列之一 系统简介
- 顺序表的各种操作
- 01 异常、异常处理
- JSP笔记之指令标识1
- 40行代码打造自己的xxxeye -- wolf
- lamp 环境搭建(非源码编译安装)
- 对模式的十大误解
- Android studio 使用帮助文档速度过慢
- 练习打字第六天!
- 四个月工作总结
- IOS开发 清空数组正确方法
- kali linux2.0安装vmware-tools方法更新版