From e86fea0ec398e8161149377a0fc5c5480ae680d0 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 24 Nov 2022 21:07:05 +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=EF=BC=9A=E5=8A=A0?= =?UTF-8?q?java=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=88=A0=E9=99=A4=E8=B4=A8?= =?UTF-8?q?=E9=87=8F=E8=BE=83=E5=B7=AE=E4=B8=94=E5=A4=9A=E4=BD=99=E7=9A=84?= =?UTF-8?q?go=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 35 ++++------------------- 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 1281b16b..904a0527 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -101,7 +101,7 @@ class Solution { int[] record = new int[26]; for (int i = 0; i < s.length(); i++) { - record[s.charAt(i) - 'a']++; + record[s.charAt(i) - 'a']++; // 并不需要记住字符a的ASCII,只要求出一个相对数值就可以了 } for (int i = 0; i < t.length(); i++) { @@ -109,11 +109,11 @@ class Solution { } for (int count: record) { - if (count != 0) { + if (count != 0) { // record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。 return false; } } - return true; + return true; // record数组所有元素都为零0,说明字符串s和t是字母异位词 } } ``` @@ -166,35 +166,10 @@ class Solution(object): Go: -```go -func isAnagram(s string, t string) bool { - if len(s)!=len(t){ - return false - } - exists := make(map[byte]int) - for i:=0;i=0&&ok{ - exists[s[i]]=v+1 - }else{ - exists[s[i]]=1 - } - } - for i:=0;i=1&&ok{ - exists[t[i]]=v-1 - }else{ - return false - } - } - return true -} -``` - -Go写法二(没有使用slice作为哈希表,用数组来代替): - ```go func isAnagram(s string, t string) bool { record := [26]int{} + for _, r := range s { record[r-rune('a')]++ } @@ -202,7 +177,7 @@ func isAnagram(s string, t string) bool { record[r-rune('a')]-- } - return record == [26]int{} + return record == [26]int{} // record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。 } ```