Plus One
- Difficulty: Easy
- Primary pattern: Arrays & Strings
- Tags: Array, Math
- Time taken: 11:48
- LeetCode Link
Key Idea
- Trick here is knowing how to reverse the range()
- To rever range(5,-1,-1) means from 5 to -1 (not including -1) in reverse order
Solution
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
# Inputs: List[int]
# Outputs: List[int]
# Description:
# Increment "digits" by one and return the list
# E.g. [1,2,3] reutrns [1,2,4]
# Increment last by 1
for i in range(len(digits) - 1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1] + digits
Complexity
- Time: O(n)
- Space: O(1)