Leetcode 66 Plus One

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.

感觉就是进位基本操作。

但是要注意 每个位子值与进位值的操作要写一行上,或者用临时变量储存。

class Solution(object):
    def plusOne(self, digits):
        c, i = 1, 1
        while c > 0 and i <= len(digits):
            digits[-i], c = (c+digits[-i])%10, (c+digits[-i])/10
            i += 1
        if c > 0:
            digits = [1] + digits
        return digits

 

posted @ 2016-06-01 15:25  lilixu  阅读(160)  评论(0编辑  收藏  举报