Question
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0‘s.
Increment the large integer by one and return the resulting array of digits.
Solution (Python)
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
curr_index = len(digits)-1
while curr_index>0:
if digits[curr_index]<9:
digits[curr_index] = digits[curr_index]+1
return digits
else:
digits[curr_index] = 0
curr_index -=1
if curr_index==0:
if digits[0]==9:
digits[0]=0
digits = [1] + digits
return digits
else:
digits[0] = 1 + digits[0]
return digits
Cracking the Coding Interview: 189 Programming Questions and Solutions