forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplus_one.py
More file actions
86 lines (67 loc) · 2.15 KB
/
plus_one.py
File metadata and controls
86 lines (67 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Plus One
Given a non-negative number represented as an array of digits (big-endian),
add one to the number and return the resulting digit array.
Reference: https://leetcode.com/problems/plus-one/
Complexity:
Time: O(n)
Space: O(n) for v1, O(1) auxiliary for v2 and v3
"""
from __future__ import annotations
def plus_one_v1(digits: list[int]) -> list[int]:
"""Add one to a big-endian digit array using manual carry propagation.
Args:
digits: Non-empty list of digits representing a non-negative integer.
Returns:
New list of digits representing the input number plus one.
Examples:
>>> plus_one_v1([1, 2, 9])
[1, 3, 0]
"""
digits[-1] = digits[-1] + 1
result = []
carry = 0
index = len(digits) - 1
while index >= 0 or carry == 1:
digit_sum = 0
if index >= 0:
digit_sum += digits[index]
if carry:
digit_sum += 1
result.append(digit_sum % 10)
carry = digit_sum // 10
index -= 1
return result[::-1]
def plus_one_v2(digits: list[int]) -> list[int]:
"""Add one to a big-endian digit array by scanning from the right.
Args:
digits: Non-empty list of digits representing a non-negative integer.
Returns:
List of digits representing the input number plus one.
Examples:
>>> plus_one_v2([1, 2, 9])
[1, 3, 0]
"""
length = len(digits)
for index in range(length - 1, -1, -1):
if digits[index] < 9:
digits[index] += 1
return digits
digits[index] = 0
digits.insert(0, 1)
return digits
def plus_one_v3(num_arr: list[int]) -> list[int]:
"""Add one to a big-endian digit array using modular arithmetic.
Args:
num_arr: Non-empty list of digits representing a non-negative integer.
Returns:
List of digits representing the input number plus one.
Examples:
>>> plus_one_v3([1, 2, 9])
[1, 3, 0]
"""
for idx in reversed(list(enumerate(num_arr))):
num_arr[idx[0]] = (num_arr[idx[0]] + 1) % 10
if num_arr[idx[0]]:
return num_arr
return [1] + num_arr