mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
更新 剑指Offer58-II.左旋转字符串.md 格式修改
更新排版
This commit is contained in:
@ -125,17 +125,21 @@ python:
|
|||||||
class Solution:
|
class Solution:
|
||||||
def reverseLeftWords(self, s: str, n: int) -> str:
|
def reverseLeftWords(self, s: str, n: int) -> str:
|
||||||
return s[n:] + s[0:n]
|
return s[n:] + s[0:n]
|
||||||
|
```
|
||||||
|
```python
|
||||||
# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法
|
# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法
|
||||||
# class Solution:
|
class Solution:
|
||||||
# def reverseLeftWords(self, s: str, n: int) -> str:
|
def reverseLeftWords(self, s: str, n: int) -> str:
|
||||||
# s = list(s)
|
s = list(s)
|
||||||
# s[0:n] = list(reversed(s[0:n]))
|
s[0:n] = list(reversed(s[0:n]))
|
||||||
# s[n:] = list(reversed(s[n:]))
|
s[n:] = list(reversed(s[n:]))
|
||||||
# s.reverse()
|
s.reverse()
|
||||||
|
|
||||||
# return "".join(s)
|
return "".join(s)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
# 方法三:如果连reversed也不让使用,那么自己手写一个
|
# 方法三:如果连reversed也不让使用,那么自己手写一个
|
||||||
class Solution:
|
class Solution:
|
||||||
def reverseLeftWords(self, s: str, n: int) -> str:
|
def reverseLeftWords(self, s: str, n: int) -> str:
|
||||||
@ -152,8 +156,10 @@ class Solution:
|
|||||||
reverse_sub(res, 0, end)
|
reverse_sub(res, 0, end)
|
||||||
return ''.join(res)
|
return ''.join(res)
|
||||||
|
|
||||||
|
# 同方法二
|
||||||
# 时间复杂度:O(n)
|
# 时间复杂度:O(n)
|
||||||
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
|
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```python 3
|
```python 3
|
||||||
|
Reference in New Issue
Block a user