From 51d8be645aab9f15101c759535785492c4162e58 Mon Sep 17 00:00:00 2001 From: Zeeland <287017217@qq.com> Date: Sat, 5 Nov 2022 18:08:43 +0800 Subject: [PATCH] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除一行不必要的print,简化for循环代码 --- problems/0242.有效的字母异位词.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index f8ebc545..b7100b52 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -123,12 +123,11 @@ Python: class Solution: def isAnagram(self, s: str, t: str) -> bool: record = [0] * 26 - for i in range(len(s)): + for i in s: #并不需要记住字符a的ASCII,只要求出一个相对数值就可以了 - record[ord(s[i]) - ord("a")] += 1 - print(record) - for i in range(len(t)): - record[ord(t[i]) - ord("a")] -= 1 + record[ord(i) - ord("a")] += 1 + for i in t: + record[ord(i) - ord("a")] -= 1 for i in range(26): if record[i] != 0: #record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。