8000 :sparkles: Added solution to 66 · huxulm/awesome-golang-leetcode@ed50c8b · GitHub
[go: up one dir, main page]

Skip to content

Commit ed50c8b

Browse files
author
Sathish Babu
committed
✨ Added solution to 66
1 parent 72ceaf9 commit ed50c8b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/0066.Plus-One/Solution.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ func plusOne(digits []int) []int {
1919
}
2020
return digits
2121
}
22+
23+
func Solution(digits []int) []int {
24+
n := len(digits)
25+
if n < 1 {
26+
return []int{1}
27+
}
28+
digits[n-1] += 1
29+
carry := 0
30+
for i := n - 1; i >= 0; i-- {
31+
n := digits[i] + carry
32+
digits[i], carry = n%10, n/10
33+
}
34+
if carry > 0 {
35+
digits = append([]int{carry}, digits...)
36+
}
37+
return digits
38+
}

src/0066.Plus-One/Solution_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package Solution
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67
)
78

@@ -25,3 +26,25 @@ func TestSolution(t *testing.T) {
2526
})
2627
}
2728
}
29+
30+
func TestSolution2(t *testing.T) {
31+
cases := []struct {
32+
name string
33+
input []int
34+
expect []int
35+
}{
36+
{"TestCase", []int{1, 2, 3}, []int{1, 2, 4}},
37+
{"TestCase", []int{9, 9, 9}, []int{1, 0, 0, 0}},
38+
{"TestCase", []int{5, 9, 9}, []int{6, 0, 0}},
39+
{"TestCase", []int{}, []int{1}},
40+
}
41+
for i, c := range cases {
42+
t.Run(c.name+" "+strconv.Itoa(i+1), func(t *testing.T) {
43+
got := Solution(c.input)
44+
if !reflect.DeepEqual(got, c.expect) {
45+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
46+
c.expect, got, c.input)
47+
}
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)
0