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

LeetCode OJ 系列之66 Plus One --Python

2015-12-09 01:32 477 查看
Problem:

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.
Answer:
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
carry_bit = 1
for i in range(len(digits)-1,-1,-1):
if digits[i]+carry_bit>9:
digits[i] = 0
else :
digits[i] = digits[i]+carry_bit
carry_bit = 0
if carry_bit == 1:
digits.insert(0,1)
return digits
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode OJ Python