Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9867724

Browse files
committed
Merge pull request #376 from 0xff-dev/1768
Add solution and test-cases for problem 1768
2 parents de7f345 + a374cdd commit 9867724

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

‎leetcode/1701-1800/1768.Merge-Strings-Alternately/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1768.Merge Strings Alternately][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)
5-
63
## Description
4+
You are given two strings `word1` and `word2`. Merge the strings by adding letters in alternating order, starting with `word1`. If a string is longer than the other, append the additional letters onto the end of the merged string.
5+
6+
Return the merged string.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: word1 = "abc", word2 = "pqr"
12+
Output: "apbqcr"
13+
Explanation: The merged string will be merged as so:
14+
word1: a b c
15+
word2: p q r
16+
merged: a p b q c r
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Merge Strings Alternately
23-
```go
21+
```
22+
Input: word1 = "ab", word2 = "pqrs"
23+
Output: "apbqrs"
24+
Explanation: Notice that as word2 is longer, "rs" is appended to the end.
25+
word1: a b
26+
word2: p q r s
27+
merged: a p b q r s
2428
```
2529

30+
**Example 3:**
31+
32+
```
33+
Input: word1 = "abcd", word2 = "pq"
34+
Output: "apbqcd"
35+
Explanation: Notice that as word1 is longer, "cd" is appended to the end.
36+
word1: a b c d
37+
word2: p q
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(word1 string, word2 string) string {
6+
sb := strings.Builder{}
7+
a, b := 0, 0
8+
for ; a < len(word1) || b < len(word2); a, b = a+1, b+1 {
9+
if a < len(word1) {
10+
sb.WriteByte(word1[a])
11+
}
12+
if b < len(word2) {
13+
sb.WriteByte(word2[b])
14+
}
15+
}
16+
return sb.String()
517
}

‎leetcode/1701-1800/1768.Merge-Strings-Alternately/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputsbool
14-
expect bool
13+
a, bstring
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abc", "pqr", "apbqcr"},
17+
{"TestCase2", "ab", "pqrs", "apbqrs"},
18+
{"TestCase3", "abcd", "pq", "apbqcd"},
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.a, c.b)
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 inputs: %v %v",
27+
c.expect, got, c.a, c.b)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /