10000 :sparkles: Added solution to 989 · ninefive/awesome-golang-leetcode@5819cbb · GitHub
[go: up one dir, main page]

Skip to content

Commit 5819cbb

Browse files
author
Sathish Babu
committed
✨ Added solution to 989
1 parent b90654b commit 5819cbb

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

src/0989.Add-to-Array-Form-of-Integer/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,40 @@
55
66
## Description
77

8+
For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1].
9+
10+
Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
11+
812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: A = [1,2,0,0], K = 34
16+
Output: [1,2,3,4]
17+
Explanation: 1200 + 34 = 1234
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: A = [2,7,4], K = 181
24+
Output: [4,5,5]
25+
Explanation: 274 + 181 = 455
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: A = [2,1,5], K = 806
32+
Output: [1,0,2,1]
33+
Explanation: 215 + 806 = 1021
34+
```
35+
36+
**Example 4:**
37+
38+
```
39+
Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
40+
Output: [1,0,0,0,0,0,0,0,0,0,0]
41+
Explanation: 9999999999 + 1 = 10000000000
1342
```
1443

1544
## 题意
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
8000 3+
func Solution(A []int, K int) []int {
4+
var res []int
5+
for i := len(A) - 1; i >= 0; i-- {
6+
res = append(res, (A[i]+K)%10)
7+
K = (A[i] + K) / 10
8+
}
9+
for K > 0 {
10+
res = append(res, K%10)
11+
K /= 10
12+
}
13+
return reverse(res)
14+
}
15+
16+
func reverse(arr []int) []int {
17+
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
18+
arr[i], arr[j] = arr[j], arr[i]
19+
}
20+
return arr
521
}

src/0989.Add-to-Array-Form-of-Integer/Solution_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
A []int
14+
K int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{1, 2, 0, 0}, 34, []int{1, 2, 3, 4}},
18+
{"TestCase", []int{0}, 0, []int{0}},
19+
{"TestCase", []int{9, 9, 9, 9, 9}, 1, []int{1, 0, 0, 0, 0, 0}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.A, c.K)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.A, c.K)
2829
}
2930
})
3031
}

0 commit comments

Comments
 (0)
0