forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup-anagrams.java
More file actions
33 lines (29 loc) · 1020 Bytes
/
Group-anagrams.java
File metadata and controls
33 lines (29 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 49. 字母异位词分组
* https://leetcode-cn.com/problems/group-anagrams/
*/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String s = new String(chars);
if (map.containsKey(s)) {
List<String> strings = map.get(s);
strings.add(str);
}else {
List<String> list = new ArrayList<>();
list.add(str);
map.put(s, list);
}
}
// PriorityQueue<List<String>> lists = new PriorityQueue<>((o1, o2) -> o1.size() - o2.size());
// lists.addAll(map.values());
// List<List<String>> lists1 = new ArrayList<>(lists.size());
// for (List<String> l: lists) {
// lists1.add(l);
// }
return new ArrayList(map.values());
}
}