[leetcode]306. Additive Number
来源:互联网 发布:吉林广电网络集团 编辑:程序博客网 时间:2024/05/22 22:35
题目链接:https://leetcode.com/problems/additive-number/#/description
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, 03
or 1, 02, 3
is invalid.
Given a string containing only digits '0'-'9'
, write a function to determine if it's an additive number.
思路:
se a helper function to add two strings.
Choose first two number then recursively check.
Note that the length of first two numbers can't be longer than half of the initial string, so the two loops in the first function will end when i>num.size()/2 and j>(num.size()-i)/2, this will actually save a lot of time.
Update the case of heading 0s
e.g. "100010" should return false
class Solution {public: bool isAdditiveNumber(string num) { for(int i=1; i<=num.size()/2; i++){ for(int j=1; j<=(num.size()-i)/2; j++){ if(check(num.substr(0,i), num.substr(i,j), num.substr(i+j))) return true; } } return false; } bool check(string num1, string num2, string num){ if(num1.size()>1 && num1[0]=='0' || num2.size()>1 && num2[0]=='0') return false; string sum=add(num1, num2); if(num==sum) return true; if(num.size()<=sum.size() || sum.compare(num.substr(0,sum.size()))!=0) return false; else return check(num2, sum, num.substr(sum.size())); } string add(string n, string m){ string res; int i=n.size()-1, j=m.size()-1, carry=0; while(i>=0 || j>=0){ int sum=carry+(i>=0 ? (n[i--]-'0') : 0) + (j>=0? (m[j--]-'0') : 0); res.push_back(sum%10+'0'); carry=sum/10; } if(carry) res.push_back(carry+'0'); reverse(res.begin(), res.end()); return res; } };
- [leetcode] 306. Additive Number
- [LeetCode]306. Additive Number
- LeetCode:306. Additive Number
- leetcode 306. Additive Number
- 306. Additive Number LeetCode
- LeetCode *** 306. Additive Number
- LeetCode 306. Additive Number
- [LeetCode]--306. Additive Number
- [leetcode] 306. Additive Number
- 【LeetCode】306. Additive Number
- leetcode 306. Additive Number
- [LeetCode]306. Additive Number
- LeetCode 306. Additive Number
- leetcode 306. Additive Number
- [leetcode]306. Additive Number
- leetcode 306. Additive Number
- (Java)LeetCode-306. Additive Number
- [leetcode] 306. Additive Number 解题报告
- js中字符串常用截取函数
- (47)按键绑定编辑器
- window.onload用法详解
- Zabbix-3.04 添加监控主机
- 383. Ransom Note
- [leetcode]306. Additive Number
- 告诉你们!我是怎么与Linux系统触电的!
- 手机/移动前端开发需要注意的20个要点
- 机房重构准备工作(4)--错误整理
- C++友元
- 商品添加-选择商品类型自动加载商品属性
- 如何解决类模板的分离编译问题?
- MongoDB远程连接 Login error的原因及解决办法 Linux shell 脚本命令
- 正则表达式