Skip to content

Commit 5e12236

Browse files
authored
Merge pull request #938 from 0xff-dev/541
Add solution and test-cases for problem 541
2 parents acfe4b1 + 36827e1 commit 5e12236

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

leetcode/501-600/0541.Reverse-String-II/README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# [541.Reverse String II][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+
Given a string `s` and an integer `k`, reverse the first `k` characters for every `2k` characters counting from the start of the string.
5+
6+
If there are fewer than `k` characters left, reverse all of them. If there are less than `2k` but greater than or equal to `k` characters, then reverse the first `k` characters and leave the other as original.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s = "abcdefg", k = 2
12+
Output: "bacdfeg"
1313
```
1414

15-
## 题意
16-
> ...
17-
18-
## 题解
15+
**Example 2:**
1916

20-
### 思路1
21-
> ...
22-
Reverse String II
23-
```go
2417
```
25-
18+
Input: s = "abcd", k = 2
19+
Output: "bacd"
20+
```
2621

2722
## 结语
2823

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, k int) string {
4+
bs := []byte(s)
5+
start := 0
6+
for start < len(s) {
7+
nextStart := start + 2*k
8+
for s, e := start, min(start+k-1, len(s)-1); s < e; s, e = s+1, e-1 {
9+
bs[s], bs[e] = bs[e], bs[s]
10+
}
11+
start = nextStart
12+
}
13+
return string(bs)
514
}

leetcode/501-600/0541.Reverse-String-II/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-
inputs bool
14-
expect bool
13+
str string
14+
k int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "abcdefg", 2, "bacdfeg"},
18+
{"TestCase2", "abcd", 2, "bacd"},
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.str, c.k)
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.str, c.k)
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)