解题思路:
排序
注意:
返回时不能用List,因为List是抽象类,return的必须是List的具体实现,如ArrayList
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] array = str.toCharArray();
Arrays.sort(array);
String key = new String(array);
List<String> list = map.getOrDefault(key, new ArrayList<String>());
list.add(str);
map.put(key, list);
}
//返回时不能用List,而必须是List的具体实现,如ArrayList
return new ArrayList<List<String>>(map.values());
}
}