306. Additive Number 自己写的,居然ac了,写一遍比看100遍强,看别人对边界的处理纯属浪费时间,不如自己动手,自己手写才能提高
2017-08-26 13:49
656 查看
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?
Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.
class Solution {
public:
bool isAdditiveNumber(string num) {
for(int i=0;i<num.size()/3+1;i++){
string s1=num.substr(0,i-0+1);
long l1=stol(s1);
string tmp=to_string(l1);
if(s1.size()==tmp.size()){
for(int j=i+1;j<num.size()/3*2+1;j++){
string s2=num.substr(i+1,j-(i+1)+1);
long l2=stol(s2);
string tmp=to_string(l2);
if(s2.size()==tmp.size()){
bool sum_flag=false;
if(dp(l1,l2,i,j,num,sum_flag))
return true;
}
}
}
}
return false;
}
bool dp(long first,long second,int i,int j,string num,bool sum_flag){
if(sum_flag==true&&(j==num.size()-1)){
return true;
}
else if(j==num.size()-1){
return false;
}
long ans=first+second;
string ans_s=to_string(ans);
int len=ans_s.size();
if(j+1+len-1>num.size()-1){
return false;
}
string num_sub=num.substr(j+1,len);
if(ans_s==num_sub){
sum_flag=true;
return dp(second,ans,j,j+1+len-1,num,sum_flag);
}
return false;
}
};
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?
Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.
class Solution {
public:
bool isAdditiveNumber(string num) {
for(int i=0;i<num.size()/3+1;i++){
string s1=num.substr(0,i-0+1);
long l1=stol(s1);
string tmp=to_string(l1);
if(s1.size()==tmp.size()){
for(int j=i+1;j<num.size()/3*2+1;j++){
string s2=num.substr(i+1,j-(i+1)+1);
long l2=stol(s2);
string tmp=to_string(l2);
if(s2.size()==tmp.size()){
bool sum_flag=false;
if(dp(l1,l2,i,j,num,sum_flag))
return true;
}
}
}
}
return false;
}
bool dp(long first,long second,int i,int j,string num,bool sum_flag){
if(sum_flag==true&&(j==num.size()-1)){
return true;
}
else if(j==num.size()-1){
return false;
}
long ans=first+second;
string ans_s=to_string(ans);
int len=ans_s.size();
if(j+1+len-1>num.size()-1){
return false;
}
string num_sub=num.substr(j+1,len);
if(ans_s==num_sub){
sum_flag=true;
return dp(second,ans,j,j+1+len-1,num,sum_flag);
}
return false;
}
};
相关文章推荐
- 很多人都患有一种毛病,将自己看得过高,自认样样都最好,而别人则个个不如自己,唯有自己看得较顺眼,这是非常错误的,因世上每个人都有些优点值得我们学习。因此我们必须“取他人之长。补自己之短”这样才能在从事涉及人事复杂的销售生意时更加胜任,更加愉快!
- 先感动自己才能感动别人
- 做最好的自己,才能碰见最好的别人
- OpenCV之imgproc 模块. 图像处理(2)实现自己的线性滤波器 给图像添加边界 Sobel 导数 Laplace 算子 Canny 边缘检测
- LeetCode | 306. Additive Number DFS
- [Leetcode] 306. Additive Number 解题报告
- 程序员如何才能提高自己?通过一次重构代码讲解自己的感受【有代码比较】
- leetcode-306 Additive Number
- 菜鸟程序员如何才能快速提高自己的技术
- DIY“物联网”——自己动手处理传感器数据
- Help_我看到别人好的文章,怎么才能够收藏到自己的博客里?_AX
- 自己动手提高ubuntu系统的性能(EXT3)
- 306. Additive Number解题报告
- 代码这东西看十行不如自己动手写一行
- 自己动手用C++写的图像处理软件(不调用外部包)
- 新手如何才能快速提高自己的技术
- 自己动手提高ubuntu系统的性能
- [LeetCode] 306. Additive Number [Medium]
- 菜鸟程序员如何才能快速提高自己的技术
- (转)怎样才能提高自己的情商和逆商水平