LeetCode | 306. Additive Number DFS
2018-01-02 13:30
381 查看
Additivenumber is a string whose digits can form additive sequence.
Avalid additive sequence should contain at least threenumbers. Except for the first two numbers, each subsequent number in thesequence must be the sum of the preceding two.
Forexample:
"112358" isan 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 additivesequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199
Note: Numbers in the additive sequence cannot haveleading zeros, so sequence 1, 2, 03 or 1, 02, 3 isinvalid.
Givena string containing only digits '0'-'9', write a function to determine if it's an additive number.
Followup:
How would you handle overflow for very large input integers?
Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.
给你一个string,问你这个string是否能转换成斐波那契数列,这题也没有特别的技巧,使用深度优先遍历解决的,以后在使用深度优先遍历的时候记得,在每一层递归的开始,一定要记得有一些值是不能改变的,假如改变这些值的话,可能会导致结果的错误,还有就是做题的时候一定要记得读题,这题我就是忽略了0的情况,导致最终的结果出现了错误
这一题我使用了一种新的技巧,就是有限层数的深度优先遍历的方法,在这里深度优先遍历我只持续两层,两层之后的计算交给循环来做,以后在做题的时候要记得这种技巧
class Solution {
public:
intgetNum(string num,int st,int ed)
{
string tmp = num.substr(st-1, ed - st +1);
return atoi(tmp.c_str());
}
bool p(intbeforeOneNum, int beforeTwoNum, int index, int t, int lengthMin, intlengthMax,int length,string num)
{
int tmp;
if(t==1||t==2)
for (int i = index + lengthMin - 1; i<= index + lengthMax - 1; i++)
{
if (t == 1)
{
beforeOneNum = -1;
beforeTwoNum = getNum(num,index, i);
if (p(beforeTwoNum, -1, i + 1,t + 1, 1, (length-(i+1)+1)/2, length, num))
return true;
else
{
if (i + 1 > index +lengthMax - 1) return false;
else
{
if(beforeTwoNum==0)return false;
continue;
}
}
}
else if (t == 2)
{
beforeTwoNum = getNum(num,index, i);
if (p(beforeOneNum,beforeTwoNum, i + 1, t + 1, max(to_string(beforeOneNum).size(), to_string(beforeTwoNum).size()),length-(i+1)+1, length, num))
return true;
else
{
if (i + 1 > index +lengthMax - 1) return false;
else
{
if (beforeTwoNum ==0) return false;
continue;
}
}
}
}
else
{
while (index + lengthMin - 1 <=length)
{
for (int i = index + lengthMin- 1; i <= index + lengthMax - 1; i++)
{
if (num[index - 1] =='0')
{
if (beforeOneNum +beforeTwoNum != 0) return false;
}
tmp = getNum(num, index,i);
if (tmp < beforeOneNum+ beforeTwoNum)
{
if (tmp == 0)return false;
continue;
}
else
{
if (tmp ==beforeOneNum + beforeTwoNum)
{
if (i ==length) return true;
index = i +1;
lengthMin =max(lengthMin, i - index + 1);
lengthMax =(length - (i + 1) + 1);
beforeOneNum= beforeTwoNum;
beforeTwoNum= tmp;
goto theNext;
}
else
{
return false;
}
}
}
return false;
theNext:;
}
return false;
}
}
bool isAdditiveNumber(stringnum) {
if (num.size() < 3) return false;
return p(-1, -1, 1, 1, 1, num.length() /2, num.length(), num);
}
};
Avalid additive sequence should contain at least threenumbers. Except for the first two numbers, each subsequent number in thesequence must be the sum of the preceding two.
Forexample:
"112358" isan 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 additivesequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199
Note: Numbers in the additive sequence cannot haveleading zeros, so sequence 1, 2, 03 or 1, 02, 3 isinvalid.
Givena string containing only digits '0'-'9', write a function to determine if it's an additive number.
Followup:
How would you handle overflow for very large input integers?
Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.
给你一个string,问你这个string是否能转换成斐波那契数列,这题也没有特别的技巧,使用深度优先遍历解决的,以后在使用深度优先遍历的时候记得,在每一层递归的开始,一定要记得有一些值是不能改变的,假如改变这些值的话,可能会导致结果的错误,还有就是做题的时候一定要记得读题,这题我就是忽略了0的情况,导致最终的结果出现了错误
这一题我使用了一种新的技巧,就是有限层数的深度优先遍历的方法,在这里深度优先遍历我只持续两层,两层之后的计算交给循环来做,以后在做题的时候要记得这种技巧
class Solution {
public:
intgetNum(string num,int st,int ed)
{
string tmp = num.substr(st-1, ed - st +1);
return atoi(tmp.c_str());
}
bool p(intbeforeOneNum, int beforeTwoNum, int index, int t, int lengthMin, intlengthMax,int length,string num)
{
int tmp;
if(t==1||t==2)
for (int i = index + lengthMin - 1; i<= index + lengthMax - 1; i++)
{
if (t == 1)
{
beforeOneNum = -1;
beforeTwoNum = getNum(num,index, i);
if (p(beforeTwoNum, -1, i + 1,t + 1, 1, (length-(i+1)+1)/2, length, num))
return true;
else
{
if (i + 1 > index +lengthMax - 1) return false;
else
{
if(beforeTwoNum==0)return false;
continue;
}
}
}
else if (t == 2)
{
beforeTwoNum = getNum(num,index, i);
if (p(beforeOneNum,beforeTwoNum, i + 1, t + 1, max(to_string(beforeOneNum).size(), to_string(beforeTwoNum).size()),length-(i+1)+1, length, num))
return true;
else
{
if (i + 1 > index +lengthMax - 1) return false;
else
{
if (beforeTwoNum ==0) return false;
continue;
}
}
}
}
else
{
while (index + lengthMin - 1 <=length)
{
for (int i = index + lengthMin- 1; i <= index + lengthMax - 1; i++)
{
if (num[index - 1] =='0')
{
if (beforeOneNum +beforeTwoNum != 0) return false;
}
tmp = getNum(num, index,i);
if (tmp < beforeOneNum+ beforeTwoNum)
{
if (tmp == 0)return false;
continue;
}
else
{
if (tmp ==beforeOneNum + beforeTwoNum)
{
if (i ==length) return true;
index = i +1;
lengthMin =max(lengthMin, i - index + 1);
lengthMax =(length - (i + 1) + 1);
beforeOneNum= beforeTwoNum;
beforeTwoNum= tmp;
goto theNext;
}
else
{
return false;
}
}
}
return false;
theNext:;
}
return false;
}
}
bool isAdditiveNumber(stringnum) {
if (num.size() < 3) return false;
return p(-1, -1, 1, 1, 1, num.length() /2, num.length(), num);
}
};
相关文章推荐
- leetcode 306. Additive Number 加法数的判断 + 一个典型的DFS深度优先遍地问题
- 306. Additive Number LeetCode
- leetcode-306 Additive Number
- [leetcode] 306. Additive Number
- [leetcode]306. Additive Number -- JavaScript 代码
- [leetcode] 306. Additive Number 解题报告
- [Leetcode] 306. Additive Number 解题报告
- leetcode_306. Additive Number ? 待解决
- leetcode 306. Additive Number
- [LeetCode] 306. Additive Number [Medium]
- 306. Additive Number | Java最短代码实现
- [leetcode] 200 Number of Islands(DFS)
- LeetCode 200 Number of Islands(DFS)
- LeetCode-17 Letter Combinations of a Phone Number(手机拨码-DFS)
- 306. Additive Number解题报告
- leetcode---letter-combinations-of-a-phone-number---dfs
- 306. Additive Number 自己写的,居然ac了,写一遍比看100遍强,看别人对边界的处理纯属浪费时间,不如自己动手,自己手写才能提高
- Additive Number-LeetCode
- LeetCode(306) Addtive Number解题报告
- Additive Number | Leetcode