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 一定是谁多了字符或者谁少了字符。 } ```