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.
Subscribe to see which companies asked this question
解题分析:
首先看到题目,没有看懂咋回事,这里给大家解释一下如何理解题意。
把一个list看成一个数字,比如说 [9, 9]
这里看成是99,然后加一,就是100,就是list , [1, 0, 0]
这里的处理方式可以从最后一位开始对每一位数组加一后进行判断是不是需要进位,
一个for如果有进位的话就加一,没有进位的话加0.
# -*- coding:utf-8 -*- __author__ = 'jiuzhang' class Solution(object): def PlusOne(self, digits): plus = 1 for i in xrange(len(digits) - 1, -1, -1): digits[i] += plus if digits[i] >= 10: digits[i] -= 10 plus = 1 else: plus = 0 break if i == 0 and plus == 1: digits.insert(0, 1) return digits
时间: 2024-10-09 01:20:39