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 carry=1;
int val=0;
for(int i=digits.length-1;i>=0;i--){
val=digits[i]+carry;
if(val>=10){
carry=1;
digits[i]=val-10;
}
else{
carry=0;
digits[i]=val;
}
}
if(carry==0) return digits;
else{
int a[]=new int[digits.length+1];
a[0]=carry;
for(int i=1;i<=digits.length;i++)
a[i]=digits[i-1];
return a;
}
}
}
410

被折叠的 条评论
为什么被折叠?



