您的位置:首页 > 其它

Additive Number-LeetCode

2015-11-29 00:22 351 查看
这是问题:

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.

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.

改了好几次重要改好了的代码:

public class Solution {
int length;
String mynum;
int next;
boolean zeroContainFlag=false;
public boolean isAdditiveNumber(final String num) {
//suppose the first number only stand for one digite
length=num.length();
if(length<3){return false;}
mynum=num;
int start1=0;
return  isAdditiveNumber(start1,start1+1);

/*        for(int start2=start1+1;start2<length;start2++){
for(int start3=start2+1;start3<length;start3++){
int result =isAddictive(start1,start2,start3);
if (result=!-1){
return false;
}else{
return  isAdditiveNumber(start2,star3);
}
}
}
*/
}

private boolean isAdditiveNumber(int start1,int start2){
for(;start2<length;start2++){
int length1=start2-start1;
System.out.println("start2 move to "+start2);
for(int start3=start2+1;(start3<length);start3++){
System.out.println("start3 move to "+start3);

zeroContainFlag=false;
int result =continueAdditive(start1,start2,start3);

if((result==-2)&&(zeroContainFlag==false)){
return true;
}else{
continue;
}

}
}
return false;
}

private int continueAdditive(int start1,int start2 ,int start3){//to jugde if the index two number have a sum in later string.
//@parameter:
//@return:the end index of the sum number starting at start3,or if none return -1
if(!(start1<start2&&start2<start3)){return -1;}
next=0;
//zeroContainFlag=false;
long sum=getLong(start1,start2-1)+getLong(start2,start3-1);//cal the sum of the specified two index
System.out.println("sum:"+getLong(start1,start2-1)+"+"+getLong(start2,start3-1)+"="+sum);
int bound=Math.max(start2-start1,start3-start2);
/*for(int i=start3+bound-1;(i<=length-1)&&((i-start3)<=bound+1);i++){//the search length can up to the longest add number added one bit.
if(getInt(start3,i)==)

}*/
if((start3+bound-1<=length-1)&&(getLong(start3,start3+bound-1)==sum)){//the sum number only exist two lenth:same length as the bound or bound length+1.
next= start3+bound-1;
}else if((start3+bound<=length-1)&&getLong(start3,start3+bound-1+1)==sum){
next=start3+bound-1+1;
}else {
next= -1;
}
if(next==length-1){
return -2;//means succeeding to find a array
}
if(next==-1){
return -1;
}

return continueAdditive(start2,start3,next+1);

}
private long getLong(int start,int end){
String temp=mynum.substring(start,end+1);
//  System.out.println(temp.substring(0,1));
if((temp.length()>1)&&(temp.substring(0,1).equals("0"))){
zeroContainFlag=true;
}
return Long.parseLong(temp);
}
}


这道题暴露了很多问题。

1. 有时候不打草稿不写代码的话思路无法清晰。但是直接写的话不清晰的思路导致的思想上的bug同样会暴露出来。所以开始的时候要把伪代码写好。检测某一种加和如“123547”在一位失败的时候并不意味着整个数字都不是,这时候要continue判断,发现其实是起始三位的additive number。

2. java的基本的语法不太清晰。比如Sting中“==”和“equal”的区别。

3. 函数的说明文档要看清楚了再用。这很重要,不要急于求成。例如String.subString()的参数中第二个参数的字符是要被截断删除的。

4. 构造递归的两个要素,终点判断,和中途传递。缺一不可。循环的终止条件也同样要考虑清楚。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string leetcode additive