66. Plus One
Python
To String and plus it
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
num = int(''.join([str(n) for n in digits]))
return [int(n) for n in str(num+1)]
Loop and carry
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
carry = 1
for i in range(len(digits)-1, -1, -1):
digits[i] += carry
if digits[i] // 10 < 1:
carry = 0
break
digits[i] %= 10
return [1] + digits if carry else digits