leetcodeOj:66. Plus One
2016-12-25 11:09
393 查看
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.
题意:一个整数按位存储于一个数组中,排列顺序为:最高位在digits[0] ,最低位在digits[n-1],
例如:10,存储为:digits[0]=1; digits[1]=0;
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
length = len(digits)
digits.reverse()
c = 1
for i in range(length):
if digits[i] == 9 and c == 1:
digits[i] = 0
c = 1
elif digits[i] != 9 and c == 1:
digits[i] += 1
c = 0
if c == 1:
digits.append(1)
digits.reverse()
return digits
The digits are stored such that the most significant digit is at the head of the list.
题意:一个整数按位存储于一个数组中,排列顺序为:最高位在digits[0] ,最低位在digits[n-1],
例如:10,存储为:digits[0]=1; digits[1]=0;
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
length = len(digits)
digits.reverse()
c = 1
for i in range(length):
if digits[i] == 9 and c == 1:
digits[i] = 0
c = 1
elif digits[i] != 9 and c == 1:
digits[i] += 1
c = 0
if c == 1:
digits.append(1)
digits.reverse()
return digits
相关文章推荐
- leetcode-66 plus one
- leetcode-66 Plus One
- Leetcode[66]-Plus One
- 【LEETCODE】66-Plus One
- 【leetcode】66. Plus One
- [LeetCode]66. Plus One
- [LeetCode]66. Plus One
- leetcode 66. Plus One
- LeetCode 66 — Plus One( Java 递归)
- leetcode--66.Plus One
- 【LeetCode】C# 66、Plus One
- [LeetCode By Python]66. Plus One
- 2018.3.12 leetcode打卡 #66 plus one
- 66 Plus One
- 66. Plus One
- [LeetCode 66] Plus One
- 66. Plus One
- 66. Plus One
- LeetCode 66 Plus One
- 66. Plus One