mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
53 lines
2.4 KiB
Markdown
53 lines
2.4 KiB
Markdown
![]() |
# [306. Additive Number](https://leetcode.com/problems/additive-number/)
|
|||
|
# 思路
|
|||
|
定义一种加法序列:除前两个数外,每个数字都是其前面两个数字的和。题目要求一个字符串序列是否可以分割成一个加法序列。
|
|||
|
|
|||
|
这题其实没有很好的方法,就是brute force。因为加法序列有一个特点:一旦前两个数确定了,那么后面每个数就确定了。所以我们暴力枚举前两个数的所有情况,然后再
|
|||
|
不断得到下一个数的值并判断得到的字符串是否和目标串相同,若是则返回True。由于follow up说了要处理int整数溢出的情况,所以我们可以使用long long型,或者自己写一个大数相加的函数(就是题[415. Add Strings](https://leetcode.com/problems/add-strings/))。
|
|||
|
|
|||
|
# C++
|
|||
|
``` C++
|
|||
|
class Solution {
|
|||
|
private:
|
|||
|
string big_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;
|
|||
|
}
|
|||
|
public:
|
|||
|
bool isAdditiveNumber(string num) {
|
|||
|
int len = num.size();
|
|||
|
for(int i = 1; i <= len / 2; i++){
|
|||
|
if(num[0] == '0' && i > 1) continue; // 前导0
|
|||
|
for(int j = 1;j <= len / 2; j++){
|
|||
|
if(num[i] == '0' && j > 1) continue; // 前导0
|
|||
|
string num1_str = num.substr(0, i);
|
|||
|
string num2_str = num.substr(i, j);
|
|||
|
string accum_str = num1_str + num2_str;
|
|||
|
|
|||
|
if(accum_str != num.substr(0, accum_str.size())) continue;
|
|||
|
while(accum_str.size() < len){
|
|||
|
// string num3_str = to_string(stol(num1_str) + stol(num2_str));
|
|||
|
string num3_str = big_add(num1_str, num2_str);
|
|||
|
|
|||
|
if(num3_str != num.substr(accum_str.size(), num3_str.size())) break;
|
|||
|
accum_str += num3_str;
|
|||
|
if(accum_str.size() == len) return true;
|
|||
|
|
|||
|
num1_str = num2_str;
|
|||
|
num2_str = num3_str;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
};
|
|||
|
```
|