From 1a82f98a175ab03569c67a436b093203ab343d20 Mon Sep 17 00:00:00 2001 From: Eyjan_Huang <81480748+Eyjan-Huang@users.noreply.github.com> Date: Thu, 19 Aug 2021 21:04:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87Offer58-?= =?UTF-8?q?II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新排版 --- .../剑指Offer58-II.左旋转字符串.md | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 6070f85b..15607a50 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -125,17 +125,21 @@ python: class Solution: def reverseLeftWords(self, s: str, n: int) -> str: return s[n:] + s[0:n] - +``` +```python # 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法 -# class Solution: -# def reverseLeftWords(self, s: str, n: int) -> str: -# s = list(s) -# s[0:n] = list(reversed(s[0:n])) -# s[n:] = list(reversed(s[n:])) -# s.reverse() +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + s = list(s) + s[0:n] = list(reversed(s[0:n])) + s[n:] = list(reversed(s[n:])) + s.reverse() + + return "".join(s) -# return "".join(s) +``` +```python # 方法三:如果连reversed也不让使用,那么自己手写一个 class Solution: def reverseLeftWords(self, s: str, n: int) -> str: @@ -152,8 +156,10 @@ class Solution: reverse_sub(res, 0, end) return ''.join(res) +# 同方法二 # 时间复杂度:O(n) # 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改 + ``` ```python 3