Given a non-negative number represented as an array of digits, plus one to the number.
The digits is stored such, the most significant digit was at the head of the list.
Test instructions: Given a number represented by an array, plus one, returns the result represented by the array
Ideas:
Refer to the solution of discuss, for each bit, only equals 9 when the carry, less than the time, the value plus one and then return to this array.
If the last value is exactly one bit more than the original value, then the new array is direct, the default value is 0, the highest assignment is 1, and returns.
The Java code is as follows:
1 Public classSolution {2 Public int[] PlusOne (int[] digits) {3 if(digits.length==0)4 returndigits;5 6 for(inti = digits.length-1; i>=0;i--) {7 if(Digits[i] < 9) {8digits[i]++;9 returndigits;Ten } OneDigits[i] = 0; A } - - int[] res =New int[Digits.length+1]; theRes[0] = 1; - returnRes; - } -}
Leetcode-66-plus One