66. Plus One [easy] (Python)

来源:互联网 发布:from unixtime mysql 编辑:程序博客网 时间:2024/06/07 03:56

题目链接

https://leetcode.com/problems/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.

题目翻译

给定一个用数组表示的非负数,对这个数加一。这个数组里的数字最高位在列表头部。

思路方法

这个题目有点难理解,应该举个例子的。。。大概就是,比如:
数字1234用数组表示是[1,2,3,4],加一后数组表示为[1,2,3,5];数字9999用数组表示是[9,9,9,9],加一得到[1,0,0,0,0]。

思路一

从后向前扫描数组,每位加一,有进位则变成0且保留进位。最高位如果是9且有进位则要在数组前面多加一个元素1。

代码

class Solution(object):    def plusOne(self, digits):        """        :type digits: List[int]        :rtype: List[int]        """        plus = 1        for i in range(len(digits)-1, -1, -1):            if digits[i] + plus > 9:                digits[i] = 0                plus = 1            else:                digits[i] = digits[i] + plus                plus = 0        if plus == 1:            digits.insert(0, 1)        return digits

思路二

当然,实际上完全不用全部数字都做判断,小于等于8的数字加一后就可以终止了。

代码

class Solution(object):    def plusOne(self, digits):        """        :type digits: List[int]        :rtype: List[int]        """        for i in range(len(digits)-1, -1, -1):            if digits[i] < 9:                digits[i] = digits[i] + 1                return digits            else:                digits[i] = 0        digits.insert(0, 1)        return digits

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51583916

0 0
原创粉丝点击