LeetCode OJ 系列之66 Plus One --Python

来源:互联网 发布:左耳最后说了什么 知乎 编辑:程序博客网 时间:2024/05/17 22:31

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

0 0
原创粉丝点击