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;
}
}
}
数值小时可通过,但是没有考虑到数值的范围。待会再改:
一把辛酸泪…………………………