Skip to content

Commit a3f0694

Browse files
committed
Merge pull request #208 from 0xff-dev/master
Add solution and test-cases for problem 415
2 parents f33256e + 0949cf7 commit a3f0694

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

leetcode/401-500/0415.Add-Strings/README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [415.Add Strings][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
53

64
## Description
5+
Given two non-negative integers, `num1` and `num2` represented as string, return the sum of `num1` and `num2` as a string.
6+
7+
You must solve the problem without using any built-in library for handling large integers (such as `BigInteger`). You must also not convert the inputs to integers directly.
8+
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: num1 = "11", num2 = "123"
14+
Output: "134"
1315
```
1416

15-
## 题意
16-
> ...
17+
__Example 2:__
1718

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Add Strings
23-
```go
19+
```
20+
Input: num1 = "456", num2 = "77"
21+
Output: "533"
2422
```
2523

24+
__Example 3__
25+
26+
```
27+
Input: num1 = "0", num2 = "0"
28+
Output: "0"
29+
```
2630

2731
## 结语
2832

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "bytes"
4+
5+
func Solution(num1 string, num2 string) string {
6+
num1Idx, num2Idx := len(num1)-1, len(num2)-1
7+
8+
buf := bytes.NewBufferString("")
9+
cf := uint8(0)
10+
for ; num1Idx >= 0 && num2Idx >= 0; num1Idx, num2Idx = num1Idx-1, num2Idx-1 {
11+
sum := num1[num1Idx] - '0' + num2[num2Idx] - '0' + cf
12+
cf = sum / 10
13+
sum %= 10
14+
buf.WriteByte(sum + '0')
15+
}
16+
17+
for ; num1Idx >= 0; num1Idx-- {
18+
sum := num1[num1Idx] - '0' + cf
19+
cf = sum / 10
20+
sum %= 10
21+
buf.WriteByte(sum + '0')
22+
}
23+
for ; num2Idx >= 0; num2Idx-- {
24+
sum := num2[num2Idx] - '0' + cf
25+
cf = sum / 10
26+
sum %= 10
27+
buf.WriteByte(sum + '0')
28+
}
29+
30+
if cf != 0 {
31+
buf.WriteByte(cf + '0')
32+
}
33+
bs := buf.Bytes()
34+
for s, e := 0, len(bs)-1; s < e; s, e = s+1, e-1 {
35+
bs[s], bs[e] = bs[e], bs[s]
36+
}
37+
38+
return string(bs)
539
}

leetcode/401-500/0415.Add-Strings/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
num1, num2 string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "11", "123", "134"},
17+
{"TestCase2", "456", "77", "533"},
18+
{"TestCase3", "0", "0", "0"},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.num1, c.num2)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with input-num1: %v, input-num2: %v",
27+
c.expect, got, c.num1, c.num2)
2828
}
2929
})
3030
}

0 commit comments

Comments
 (0)