File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,35 @@ class Solution {
152
152
}
153
153
}
154
154
```
155
+ ``` java
156
+ // 解法3
157
+ class Solution {
158
+ public String reverseStr (String s , int k ) {
159
+ char [] ch = s. toCharArray();
160
+ // 1. 每隔 2k 个字符的前 k 个字符进行反转
161
+ for (int i = 0 ; i< ch. length; i += 2 * k) {
162
+ // 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
163
+ if (i + k <= ch. length) {
164
+ reverse(ch, i, i + k - 1 );
165
+ continue ;
166
+ }
167
+ // 3. 剩余字符少于 k 个,则将剩余字符全部反转
168
+ reverse(ch, i, ch. length - 1 );
169
+ }
170
+ return new String (ch);
155
171
172
+ }
173
+ // 定义翻转函数
174
+ public void reverse (char [] ch , int i , int j ) {
175
+ for (; i < j; i++ , j-- ) {
176
+ char temp = ch[i];
177
+ ch[i] = ch[j];
178
+ ch[j] = temp;
179
+ }
180
+
181
+ }
182
+ }
183
+ ```
156
184
Python:
157
185
``` python
158
186
class Solution :
You can’t perform that action at this time.
0 commit comments