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.
改了好几次重要改好了的代码:
这道题暴露了很多问题。
1. 有时候不打草稿不写代码的话思路无法清晰。但是直接写的话不清晰的思路导致的思想上的bug同样会暴露出来。所以开始的时候要把伪代码写好。检测某一种加和如“123547”在一位失败的时候并不意味着整个数字都不是,这时候要continue判断,发现其实是起始三位的additive number。
2. java的基本的语法不太清晰。比如Sting中“==”和“equal”的区别。
3. 函数的说明文档要看清楚了再用。这很重要,不要急于求成。例如String.subString()的参数中第二个参数的字符是要被截断删除的。
4. 构造递归的两个要素,终点判断,和中途传递。缺一不可。循环的终止条件也同样要考虑清楚。
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. 构造递归的两个要素,终点判断,和中途传递。缺一不可。循环的终止条件也同样要考虑清楚。
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法
- C#中string用法实例详解
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码
- JavaScript中的object转换成number或string规则介绍