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 1a82f98

Browse files
更新 剑指Offer58-II.左旋转字符串.md 格式修改
更新排版
1 parent 6255eae commit 1a82f98

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,21 @@ python:
125125
class Solution:
126126
def reverseLeftWords(self, s: str, n: int) -> str:
127127
return s[n:] + s[0:n]
128-
128+
```
129+
```python
129130
# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法
130-
# class Solution:
131-
# def reverseLeftWords(self, s: str, n: int) -> str:
132-
# s = list(s)
133-
# s[0:n] = list(reversed(s[0:n]))
134-
# s[n:] = list(reversed(s[n:]))
135-
# s.reverse()
131+
class Solution:
132+
def reverseLeftWords(self, s: str, n: int) -> str:
133+
s = list(s)
134+
s[0:n] = list(reversed(s[0:n]))
135+
s[n:] = list(reversed(s[n:]))
136+
s.reverse()
137+
138+
return "".join(s)
136139

137-
# return "".join(s)
140+
```
138141

142+
```python
139143
# 方法三:如果连reversed也不让使用,那么自己手写一个
140144
class Solution:
141145
def reverseLeftWords(self, s: str, n: int) -> str:
@@ -152,8 +156,10 @@ class Solution:
152156
reverse_sub(res, 0, end)
153157
return ''.join(res)
154158

159+
# 同方法二
155160
# 时间复杂度:O(n)
156161
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
162+
157163
```
158164

159165
```python 3

0 commit comments

Comments
(0)

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