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

leetcodeOj:66. Plus One

2016-12-25 11:09 393 查看
Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.
题意:一个整数按位存储于一个数组中,排列顺序为:最高位在digits[0] ,最低位在digits[n-1],  
例如:10,存储为:digits[0]=1; digits[1]=0;  

class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
length = len(digits)
digits.reverse()
c = 1
for i in range(length):
if digits[i] == 9 and c == 1:
digits[i] = 0
c = 1
elif digits[i] != 9 and c == 1:
digits[i] += 1
c = 0
if c == 1:
digits.append(1)
digits.reverse()
return digits
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode OJ python