加一(66)
法一: class Solution: def plusOne(self, digits: List[int]) -> List[int]: length = len(digits) res = 0 for i in digits: if digits[-1]!=9: if length!=1: res+=i*int('1'+'0'*(length-1)) length-=1 else: res += (i+1) else: if length!=1: res+=i*int('1'+'0'*(length-1)) length-=1 else: res += 10 res = [int(num) for num in str(res)] return res
法二:
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
newlst = []
while digits and digits[-1] == 9:
digits.pop()
newlst.append(0)
if not digits:
return [1] + newlst
else:
digits[-1] += 1
return digits + newlst