66. Plus One
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.public class Solution {
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
digits[i] += 1;
if (digits[i] <= 9) return digits;
digits[i] = 0;
}
int[] res = new int[digits.length + 1];
res[0] = 1;
return res;
}
}Last updated