You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0500-0599/0541.Reverse String II/README_EN.md
+40-18
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,11 @@ tags:
44
44
45
45
<!-- solution:start -->
46
46
47
-
### Solution 1
47
+
### Solution 1: Two Pointers
48
+
49
+
We can traverse the string $\textit{s}$, iterating over every $\textit{2k}$ characters, and then use the two-pointer technique to reverse the first $\textit{k}$ characters among these $\textit{2k}$ characters.
50
+
51
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{s}$.
48
52
49
53
<!-- tabs:start -->
50
54
@@ -53,26 +57,27 @@ tags:
53
57
```python
54
58
classSolution:
55
59
defreverseStr(self, s: str, k: int) -> str:
56
-
t=list(s)
57
-
for i inrange(0, len(t), k <<1):
58
-
t[i : i + k] =reversed(t[i : i + k])
59
-
return''.join(t)
60
+
cs=list(s)
61
+
for i inrange(0, len(cs), 2* k):
62
+
cs[i : i + k] =reversed(cs[i : i + k])
63
+
return"".join(cs)
60
64
```
61
65
62
66
#### Java
63
67
64
68
```java
65
69
classSolution {
66
70
publicStringreverseStr(Strings, intk) {
67
-
char[] chars = s.toCharArray();
68
-
for (int i =0; i < chars.length; i += (k <<1)) {
69
-
for (int st = i, ed =Math.min(chars.length -1, i + k -1); st < ed; ++st, --ed) {
70
-
char t = chars[st];
71
-
chars[st] = chars[ed];
72
-
chars[ed] = t;
71
+
char[] cs = s.toCharArray();
72
+
int n = cs.length;
73
+
for (int i =0; i < n; i += k *2) {
74
+
for (int l = i, r =Math.min(i + k -1, n -1); l < r; ++l, --r) {
75
+
char t = cs[l];
76
+
cs[l] = cs[r];
77
+
cs[r] = t;
73
78
}
74
79
}
75
-
returnnewString(chars);
80
+
returnnewString(cs);
76
81
}
77
82
}
78
83
```
@@ -83,7 +88,8 @@ class Solution {
83
88
classSolution {
84
89
public:
85
90
string reverseStr(string s, int k) {
86
-
for (int i = 0, n = s.size(); i < n; i += (k << 1)) {
91
+
int n = s.size();
92
+
for (int i = 0; i < n; i += 2 * k) {
87
93
reverse(s.begin() + i, s.begin() + min(i + k, n));
88
94
}
89
95
return s;
@@ -95,13 +101,29 @@ public:
95
101
96
102
```go
97
103
func reverseStr(s string, k int) string {
98
-
t := []byte(s)
99
-
for i := 0; i < len(t); i += (k << 1) {
100
-
for st, ed := i, min(i+k-1, len(t)-1); st < ed; st, ed = st+1, ed-1 {
101
-
t[st], t[ed] = t[ed], t[st]
104
+
cs := []byte(s)
105
+
n := len(cs)
106
+
for i := 0; i < n; i += 2 * k {
107
+
for l, r := i, min(i+k-1, n-1); l < r; l, r = l+1, r-1 {
108
+
cs[l], cs[r] = cs[r], cs[l]
102
109
}
103
110
}
104
-
return string(t)
111
+
return string(cs)
112
+
}
113
+
```
114
+
115
+
#### TypeScript
116
+
117
+
```ts
118
+
function reverseStr(s:string, k:number):string {
119
+
const n =s.length;
120
+
const cs =s.split('');
121
+
for (let i =0; i<n; i+=2*k) {
122
+
for (let l =i, r =Math.min(i+k-1, n-1); l<r; l++, r--) {
0 commit comments