Title Description:
Given a non-negative number, represents a numeric array, on the basis of that number +1, returns a new array.
The number is arranged by size, and the largest number is at the front of the list.
Have you ever encountered this problem in a real interview? Yes
Sample Example
Given [1,2,3]
a representation of 123, returns [1,2,4]
.
Given [9,9,9]
a representation of 999, returns [1,0,0,0]
.
labelArray of Google
Topic Analysis:
The end [-1] plus 1, and then reverse the loop, determine whether the element is 10, and then processing;
Special handling, [0] element is 10, need to insert a 1 in the first place.
Source:
class Solution: # @param {int[]} digits a number represented as an array of Digi TS # @return {int[]} The result def plusone (self, digits): # Write Your code here n = len (digits) DIGITS[-1] + = 1 for i in Range ( -1,-n,-1): if digits[i] = = 10:digits[i] = 0 DIGITS[I-1] + = 1 if digits[0] = = 10:digits[0] = 0 Digits.insert (0,1) return Digi TS def _plusone (self, digits): # Write Your code here Digits.reverse () n = len (digits) Digits[0] + = 1 for i in range (n): if digits[i] = = 10:digits[i] = 0 if i = = n-1: Digits.append (1) else:digits[i+1] + = 1 if digits[-1] = = 10:digits[-1] = 0 digits.append (1) digits.reverse () return digits
Lintcode Python Simple Class topic 407. Add one