您的位置:首页 > 编程语言 > Python开发

66. Plus One [python]

2018-02-28 22:20 267 查看
题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are stored such that the most significant digit is at the head of the list.就是说一组数表示成数组形式,然后加一把她仍写成数组,如999 + 1 = 1000, [9, 9, 9]-->[1, 0, 0, 0]程序:class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
N = len(digits)
i = N-1

while digits[i] == 9:
digits[i] = 0

if i == 0:
digits.insert(0,1)
return digits
else:
i -= 1

digits[i] += 1
return digits
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: