From f6eee532e49acba882be2b202825ee7082d9a69c Mon Sep 17 00:00:00 2001 From: Eyjan_Huang <81480748+Eyjan-Huang@users.noreply.github.com> Date: Thu, 19 Aug 2021 16:26:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200344.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20python=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E5=88=A0=E9=99=A4=E5=8F=A6=E4=B8=80?= =?UTF-8?q?=E7=A7=8D=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 另一种解法进行了不必要的操作,且代码可读性较差。 推荐使用第一种解法 --- problems/0344.反转字符串.md | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 4f96f839..763097be 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -162,21 +162,14 @@ class Solution: Do not return anything, modify s in-place instead. """ left, right = 0, len(s) - 1 - while(left < right): + + # 该方法已经不需要判断奇偶数,经测试后时间空间复杂度比用 for i in range(right//2)更低 + # 推荐该写法,更加通俗易懂 + while left < right: s[left], s[right] = s[right], s[left] left += 1 right -= 1 - -# 下面的写法更加简洁,但是都是同样的算法 -# class Solution: -# def reverseString(self, s: List[str]) -> None: -# """ -# Do not return anything, modify s in-place instead. -# """ - # 不需要判别是偶数个还是奇数个序列,因为奇数个的时候,中间那个不需要交换就可 -# for i in range(len(s)//2): -# s[i], s[len(s)-1-i] = s[len(s)-1-i], s[i] -# return s + ``` Go: