https://leetcode-cn.com/problems/plus-one/description/
我的解决方案:
class Solution {
public int[] plusOne(int[] digits) {
//carry用来存储进位
int carry = 0;
int tmp = 0;
int once=1;
for(int i=digits.length-1;i>=0;i--) {
tmp = digits[i]+carry+once;
digits[i]=tmp%10;
carry=tmp/10;
once=0;
}
if(carry==1) {
int[] res = new int[1+digits.length];
res[0]=1;
for(int i=0;i<digits.length;i++){
res[i+1]=digits[i];
}
return res;
}
return digits;
}
}