forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlus_One.py
More file actions
20 lines (18 loc) · 710 Bytes
/
Plus_One.py
File metadata and controls
20 lines (18 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""
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.
"""
class Solution:
# @param digits, a list of integer digits
# @return a list of integer digits
def plusOne(self, digits):
i = len(digits) - 1
carry = 1
while i >= 0 and carry == 1: # So many detail! No need to continue calculation if carry == 0
s = digits[i] + carry # Calculate s first
digits[i] = s % 10
carry = s / 10
i -= 1
if carry == 1: # Last check
digits.insert(0, 1)
return digits