视频教程:B站、网易云课堂、腾讯课堂
代码地址:Gitee、Github
Google云
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
The idea is simple. It creates a size 26 int arrays as buckets for each letter in alphabet. It increments the bucket value with String s and decrement with string t. So if they are anagrams, all buckets should remain with initial value which is zero. So just checking that and return
java版
/*
思路:
*/
public class Solution {
public boolean isAnagram(String s, String t) {
int[] alphabet = new int[26];
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
for (int i : alphabet) if (i != 0) return false;
return true;
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int i = 0;i<numbers.length;i++) {
if (map.containsKey(target-numbers[i])) {
result[1] = i;
result[0] = map.get(target-numbers[i]);
return result;
}
map.put(numbers[i],i);
}
return result;
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) return new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] ca = s.toCharArray();
Arrays.sort(ca);
String keyStr = String.valueOf(ca);
if (!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<>());
map.get(keyStr).add(s);
}
return new ArrayList<>(map.values());
}
//Instead of sorting, we can also build the key string in this way. Thanks @davidluoyes for pointing this out.
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) return new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] ca = new char[26];
for (char c : s.toCharArray()) ca[c - 'a']++;
String keyStr = String.valueOf(ca);
if (!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<>());
map.get(keyStr).add(s);
}
return new ArrayList<>(map.values());
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
一 哈希表、映射、集合的实现与特性
二 实战题目解析:有效的字母异位词等问题