题目描述
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) {
if (digits == null || digits.length == 0) {
return null;
}
int[] reslut = new int[digits.length + 1];
digits[digits.length - 1]++;
for (int i = digits.length - 1; i >= 0; i--) {
reslut[i + 1] += digits[i];
reslut[i] += reslut[i + 1] / 10;
reslut[i + 1] %= 10;
}
if (reslut[0] == 0) {
return Arrays.copyOfRange(reslut, 1, reslut.length);
} else {
return reslut;
}
}
}