66. Plus One

https://leetcode.com/problems/plus-one/description/
解题思路:
本位为9则进位,否则直接+1

代码:
class Solution {
public int[] plusOne(int[] digits) {
if (digits == null) return new int[1];
for(int i = digits.length - 1; i >= 0; i--){
if(digits[i] != 9){
digits[i] += 1;
return digits;
} else {
digits[i] = 0;
}
}
int[] res = new int[digits.length + 1];
res[0] = 1;
return res;
}
}

你可能感兴趣的:(66. Plus One)