这一题比较简单:
思路就是因为list可能会增加一位数,所以为了方便增加位数,先把list reverse然后加1以后再reverse回来,代码如下:
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
lenth = len(digits)
digits.reverse()
i = 0
while i < len(digits):
if digits[i] <= 8:
digits[i] += 1
break
elif i != lenth - 1 and digits[i] == 9:
digits[i] = 0
i += 1
elif i == lenth - 1 and digits[i] == 9:
digits[i] = 0
digits.append(1)
break
digits.reverse()
return digits