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 6255eae

Browse files
补充 剑指Offer58-II.左旋转字符串.md python方法
如果不让使用自带函数reversed() 可以使用该方法
1 parent 51e719a commit 6255eae

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,28 @@ class Solution:
136136

137137
# return "".join(s)
138138

139+
# 方法三:如果连reversed也不让使用,那么自己手写一个
140+
class Solution:
141+
def reverseLeftWords(self, s: str, n: int) -> str:
142+
def reverse_sub(lst, left, right):
143+
while left < right:
144+
lst[left], lst[right] = lst[right], lst[left]
145+
left += 1
146+
right -= 1
147+
148+
res = list(s)
149+
end = len(res) - 1
150+
reverse_sub(res, 0, n - 1)
151+
reverse_sub(res, n, end)
152+
reverse_sub(res, 0, end)
153+
return ''.join(res)
139154

140155
# 时间复杂度:O(n)
141156
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
142157
```
143158

144159
```python 3
145-
#方法三:考虑不能用切片的情况下,利用模+下标实现
160+
#方法四:考虑不能用切片的情况下,利用模+下标实现
146161
class Solution:
147162
def reverseLeftWords(self, s: str, n: int) -> str:
148163
new_s = ''

0 commit comments

Comments
(0)

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