# Q66 Plus One

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.

##### Python实现：
```class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
if digits[-1] != 9:  # 如果各位不为9，则直接加1即可
digits[-1] += 1
return digits
else:
i = len(digits) - 1
bit = 1
while i >= 0:
if digits[i] + bit == 10:
digits[i] = 0
bit = 1
else:
digits[i] += 1
bit = 0
break  # 不再向前进位
i -= 1
if bit == 1:  # 如果最前面仍然产生进位，则插入1，比如 99 -> 100
digits.insert(0,1)
return digits

a = [2,4,9,3,9]
b = Solution()  # 24940
print(b.plusOne(a))```

161 篇文章42 人订阅

0 条评论

## 相关文章

1895

772

1111

2477

904

1K2

### Q169 Majority Element

Given an array of size n, find the majority element. The majority element is the...

4147

4091

721

### 剑指Offer面试题：9.二进制中1的个数

一个基本的思路：先判断整数二进制表示中最右边一位是不是1。接着把输入的整数右移一位，此时原来处于从右边数起的第二位被移到最右边了，再判断是不是1。这样每次移...

752