GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/L049_Group_Anagrams.java at master · likevson/LeetCode

Expand file tree

/

L049_Group_Anagrams.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
57 lines (44 loc) · 1.17 KB
/

L049_Group_Anagrams.java

Copy path

File metadata and controls

57 lines (44 loc) · 1.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package LeetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class L049_Group_Anagrams {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) {
return new ArrayList<List<String>>();
}
List<List<String>> rt = new ArrayList<List<String>>();
Map<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
// 把单词分组
for (int i = 0; i < strs.length; i++) {
char[] c = strs[i].toCharArray();
Arrays.sort(c);
String k = Arrays.toString(c);
ArrayList<Integer> list = new ArrayList<Integer>();
if (map.containsKey(k)) {
list = map.get(k);
}
list.add(i);
map.put(k, list);
}
for (String s : map.keySet()) {
List<Integer> l = map.get(s);
List<String> group = new ArrayList<String>();
// 把相同字母的单词放入同一个list
for (Integer i : l) {
group.add(strs[i]);
}
// 按字典序排序
group.sort(new Comparator<String>() {
public int compare(String x, String y) {
return x.compareTo(y);
}
});
rt.add(group);
}
return rt;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.