Additive Number-LeetCode

这是问题:

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. 构造递归的两个要素,终点判断,和中途传递。缺一不可。循环的终止条件也同样要考虑清楚。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小马工匠坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值