Skip to content

Commit ffbe980

Browse files
Merge pull request #445 from borninfreedom/master
update左旋转字符串,添加python代码
2 parents 028d8ff + 3dc4167 commit ffbe980

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ class Solution {
118118
}
119119
```
120120

121+
```python
122+
# 方法一:可以使用切片方法
123+
class Solution:
124+
def reverseLeftWords(self, s: str, n: int) -> str:
125+
return s[n:] + s[0:n]
126+
127+
# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法
128+
# class Solution:
129+
# def reverseLeftWords(self, s: str, n: int) -> str:
130+
# s = list(s)
131+
# s[0:n] = list(reversed(s[0:n]))
132+
# s[n:] = list(reversed(s[n:]))
133+
# s.reverse()
134+
135+
# return "".join(s)
136+
137+
138+
# 时间复杂度:O(n)
139+
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
140+
```
141+
121142
Go:
122143

123144
```go

0 commit comments

Comments
 (0)