Skip to content

Commit ed8ae23

Browse files
committed
66. Plus One
1 parent 80dac89 commit ed8ae23

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

plus-one/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 66. Plus One
2+
3+
Source: https://leetcode.com/problems/plus-one/description/

plus-one/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leecode
2+
3+
func plusOne(digits []int) []int {
4+
plusOne := true
5+
// start from the last element
6+
for i := len(digits) - 1; i >= 0; i-- {
7+
if digits[i] < 9 {
8+
digits[i]++
9+
return digits
10+
}
11+
digits[i] = 0
12+
}
13+
if plusOne {
14+
digits = append([]int{1}, digits...)
15+
}
16+
return digits
17+
}

plus-one/main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leecode
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestDominantIndex(t *testing.T) {
10+
testCases := []struct {
11+
Input []int
12+
Output []int
13+
}{
14+
{
15+
Input: []int{9},
16+
Output: []int{1, 0},
17+
},
18+
}
19+
for _, tc := range testCases {
20+
t.Run(fmt.Sprintf("%v:%d", tc.Input, tc.Output), func(t *testing.T) {
21+
actual := plusOne(tc.Input)
22+
if !reflect.DeepEqual(actual, tc.Output) {
23+
t.Errorf("expected %d, but get %d", tc.Output, actual)
24+
}
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)