您的位置:首页 > 其它

LeetCode_66. Plus One_数组加一

2016-01-21 16:21 393 查看
66. Plus One

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.
public class Solution {
public int[] plusOne(int[] digits) {
int len = digits.length;
int sum = 0;
int sum1 = 0;
int[] res= new int[len];
int[] res1= new int[len+1];
for(int i = 0; i< digits.length; i++){
sum +=Math.pow(10,len-i-1)*digits[i];
}
for(int i = 0; i<digits.length; i++){
sum1 += Math.pow(10,len-i-1)*1;
}

if(sum1*9==sum){
sum= sum+1;
res1[0]=1;
for(int j=1;j<res1.length;j++){
res1[j]=0;
}
return res1;
}else{

for(int j=0;j<res.length;j++){
res[res.length-j-1]=sum%10;
sum=sum/10;
}
return res;
}

}
}

数值小时可通过,但是没有考虑到数值的范围。待会再改:

一把辛酸泪…………………………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: