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 b24e3f0

Browse files
update:剑指Offer58-II.左旋转字符串.md
解法二:空间复杂度:O(1)。用原始数组来进行反转操作 思路:先整个字符串反转,再反转前面的,最后反转后面 n 个
1 parent 42d72e2 commit b24e3f0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎problems/剑指Offer58-II.左旋转字符串.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ class Solution {
115115
}
116116
```
117117

118+
```java
119+
//解法二:空间复杂度:O(1)。用原始数组来进行反转操作
120+
//思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个
121+
class Solution {
122+
public String reverseLeftWords(String s, int n) {
123+
char[] chars = s.toCharArray();
124+
reverse(chars, 0, chars.length - 1);
125+
reverse(chars, 0, chars.length - 1 - n);
126+
reverse(chars, chars.length - n, chars.length - 1);
127+
return new String(chars);
128+
}
129+
130+
public void reverse(char[] chars, int left, int right) {
131+
while (left < right) {
132+
chars[left] ^= chars[right];
133+
chars[right] ^= chars[left];
134+
chars[left] ^= chars[right];
135+
left++;
136+
right--;
137+
}
138+
}
139+
```
140+
118141
python:
119142

120143
```python

0 commit comments

Comments
(0)

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