From 0c4a4a68ce149d664226f836b8ebf8709cd49d08 Mon Sep 17 00:00:00 2001 From: shuwen Date: Sat, 14 Aug 2021 17:38:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=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=20Pyt?= =?UTF-8?q?hon3=E7=89=88=E6=9C=AC=20=E4=BD=BF=E7=94=A8=E6=A8=A1+=E4=B8=8B?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 考虑不能用切片的情况下,利用模+下标实现 --- problems/剑指Offer58-II.左旋转字符串.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 76908f2d..1073dafa 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -141,6 +141,18 @@ class Solution: # 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改 ``` +```python 3 +#方法三:考虑不能用切片的情况下,利用模+下标实现 +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + new_s = '' + for i in range(len(s)): + j = (i+n)%len(s) + new_s = new_s + s[j] + return new_s + +``` + Go: ```go