[leetcode:python]66.Plus One
2017-05-10 20:20
281 查看
题目:
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
题意:
给定一个非负数,用非空数组表示,对这个非负数加一,把和存到这个数组。
方法一:性能46ms
方法二:性能35ms
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
题意:
给定一个非负数,用非空数组表示,对这个非负数加一,把和存到这个数组。
方法一:性能46ms
class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ digits[-1] += 1 if digits[-1] < 10: return digits s = [] while digits[-1] == 10: s.append(0) digits.pop() if len(digits) == 0: s[0] = 1 s.append(0) return s digits[-1] += 1 while s: digits.append(s.pop(0)) return digits
方法二:性能35ms
class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ for i in xrange(len(digits)-1, -1, -1): if digits[i]<9: digits[i] = digits[i] + 1; return digits; else: digits[i] = 0; if digits[0] == 0: digits.insert(0,1); return digits;
相关文章推荐
- LeetCode 66 Plus One (Python详解及实现)
- LeetCode 66 — Plus One(C++ Java Python)
- [LeetCode By Python]66. Plus One
- [Leetcode]@python 66. Plus One
- 【LeetCode 66】Plus One(Python)
- LeetCode66. Plus One-python(easy)
- leetcode-66 plus one
- [LeetCode]66 Plus One
- leetcode 【 Plus One 】python 实现
- [leetcode]Plus One @ Python
- 【leetcode c++】66 Plus One
- leetcode ||66、 Plus One
- 【LeetCode with Python】 Plus One
- leetcode 66:plus one
- 【leetcode】【66】Plus One
- leetcode[66]:Plus One
- Plus One - LeetCode 66
- [leetcode 66] Plus One
- LeetCode 66 Plus One
- LeetCode66——Plus One