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 static int[] plusOne(int[] digits) {
int sub=0;
if(digits.length==1){
digits[0]+=1;
if(digits[0]>9){
int[] temp=new int[2];
temp[0]=1;
temp[1]=digits[0]-10;
return temp;
}
}
for(int i=digits.length-1;i>0;i--){
if(sub==1||i==digits.length-1)
digits[i]+=1;
if(digits[i]>9){
sub=1;
digits[i]-=10;
}else{
sub=0;
}
}
if(sub==1) {
digits[0]+=1;
if(digits[0]>9){
int[] temp=new int[digits.length+1];
temp[0]=1;
digits[0]-=10;
for(int i=1;i<temp.length;i++){
temp[i]=digits[i-1];
}
return temp;
}
}
return digits;
}
}