You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
'''
|
|
|
|
|
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
|
|
|
|
|
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
|
|
|
|
|
你可以假设除了整数 0 之外,这个整数不会以零开头。
|
|
|
|
|
示例 1:输入:[1,2,3] 输出:[1,2,4] 解释:输入数组表示数字 123。
|
|
|
|
|
示例 2:输入:[4,3,2,1] 输出:[4,3,2,2] 解释:输入数组表示数字 4321。
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
class Solution(object):
|
|
|
|
|
def _plusOne(self, digits):
|
|
|
|
|
"""
|
|
|
|
|
:type digits: List[int]
|
|
|
|
|
:rtype: List[int]
|
|
|
|
|
"""
|
|
|
|
|
carry = 1
|
|
|
|
|
for i in reversed(xrange(0,len(digits))):
|
|
|
|
|
digit = (digits[i] + carry) % 10
|
|
|
|
|
carry = 1 if digit < digits[i] else 0
|
|
|
|
|
digits[i] = digit
|
|
|
|
|
if carry == 1:
|
|
|
|
|
return [1] + digits
|
|
|
|
|
return digits
|
|
|
|
|
|
|
|
|
|
# 一句话写法,在int和str之间转换
|
|
|
|
|
def plusOne(self, digits):
|
|
|
|
|
return [int(j) for j in str(int(''.join('%s' % i for i in digits))+1)]
|