-
Notifications
You must be signed in to change notification settings - Fork 0
/
plus_one_test.go
86 lines (71 loc) · 1.84 KB
/
plus_one_test.go
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
/*
Problem:
- Given a number represented as an array of digits, plus one to the number.
Assumption:
- The input are non-negative.
- The digits are stored such that the most significant digit is at the head of the list.
- The number does not contain leading zeros.
Example:
- Input: []int{1, 2, 5}
Output: []int{1, 2, 6}
- Input: []int{1, 2, 9}
Output: []int{1, 3, 0}
- Input: []int{1, 9, 9}
Output: []int{2, 0, 0}
Solution:
- Iterate through the list from right to left and add 1 to the current digit accordingly.
- If the current digit is less than 9, add 1 and update it.
- Otherwise, set it to 0.
- If all the digits are 9, append an 0 in the end and update the first digit to 1.
Cost:
- O(n) time, O(1) space, where n is the length of the list.
*/
package leetcode
import (
"testing"
"github.com/pcaruana/algo/common"
)
func TestPlusOne(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{0}, []int{1}},
{[]int{1}, []int{2}},
{[]int{9}, []int{1, 0}},
{[]int{1, 2, 5}, []int{1, 2, 6}},
{[]int{1, 2, 9}, []int{1, 3, 0}},
{[]int{1, 9, 9}, []int{2, 0, 0}},
{[]int{9, 9, 9}, []int{1, 0, 0, 0}},
}
for _, tt := range tests {
result := plusOne(tt.in)
common.Equal(t, tt.expected, result)
}
}
func plusOne(digits []int) []int {
// check edge case to see if the input is empty.
if len(digits) == 0 {
return digits
}
// counter counts the number of 9 appears in the original input.
counter := 0
// iterate from right to left and add 1 to the current digit accordingly.
for i := len(digits) - 1; i > -1; i-- {
d := digits[i]
if d < 9 {
digits[i] = d + 1
break
} else {
digits[i] = 0
counter++
}
}
// if all digits are 9, add an 0 in the end and change the digit to 1.
if counter == len(digits) {
digits = append(digits, 0)
digits[0] = 1
}
return digits
}