LeetCode Plus One
LeetCode solving Plus One
Original question
Add a non-negative integer that consists of a list of numbers.
Note:
The number before the list indicates that the highest bit may also be carried.
Example:
Input: [1, 2, 3, 4, 9]
Output: [1, 2, 3, 5, 0]
Solutions
From the low position to the high position. If the last digit has a forward position, add this digit. Otherwise, exit the loop. If the highest bit is also carried, insert one before the list.
AC Source Code
Class Solution (object): def plusOne (self, digits): "": type digits: List [int]: rtype: list [int] "" carry = 1 for I in range (len (digits)-1,-1,-1 ): digits [I] + = carry if digits [I] <10: carry = 0 break else: digits [I]-= 10 if carry = 1: digits. insert (0, 1) return digitsif _ name _ = "_ main _": assert Solution (). plusOne ([1, 2, 3, 4, 9]) = [1, 2, 3, 5, 0] assert Solution (). plusOne ([9]) = [1, 0]