题目传送地址:https://leetcode.cn/problems/plus-one/
运行效率:
代码如下:
public static int[] plusOne(int[] digits) {
int lastIndex = digits.length - 1;
digits[lastIndex]++;//末尾的数加1
while (lastIndex > 0 && digits[lastIndex] == 10) {
digits[lastIndex] = 0;
lastIndex--;
digits[lastIndex]++;
}
if (lastIndex == 0 && digits[0] == 10) { //比如 999 如果再加1的话,那就是1000
digits[0]=0;
int[] res = new int[digits.length + 1];
res[0] = 1;
System.arraycopy(digits, 0, res, 1, digits.length);
return res;
}
return digits;
}