Skip to content

Commit 5959fa5

Browse files
Merge pull request #476 from AndroidQin/master
【第二周】
2 parents 9519dbf + 0ddb566 commit 5959fa5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Week_02/id_95/LeetCode_609_95.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class LeetCode_609_95 {
2+
public List<List<String>> findDuplicate(String[] paths) {
3+
List<List<String>> ans = new ArrayList(200);
4+
HashMap<String,List<String>>map = new HashMap(500);
5+
for(String path:paths)
6+
{
7+
String[] temp = path.split(" ");
8+
String root = temp[0];
9+
for(int i = 1;i < temp.length;i++)
10+
{
11+
int begin = temp[i].indexOf("(");
12+
int end = temp[i].lastIndexOf(")");
13+
String name = temp[i].substring(begin+1,end);
14+
15+
String s = root+"/"+temp[i].substring(0,begin);
16+
if(map.get(name) == null)
17+
{
18+
List<String> list = new ArrayList(10);
19+
list.add(s);
20+
map.put(name,list);
21+
}
22+
else map.get(name).add(s);
23+
}
24+
}
25+
for(Map.Entry<String,List<String> > entry:map.entrySet())
26+
if(entry.getValue().size()>1)
27+
ans.add(entry.getValue());
28+
29+
return ans;
30+
}
31+
}
32+

Week_02/id_95/LeetCode_692_95.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class LeetCode_692_95 {
2+
public List<String> topKFrequent(String[] words, int k) {
3+
Map<String,Integer> countMap = new HashMap<>();
4+
for(String word:words)
5+
countMap.put(word,countMap.getOrDefault(word,0)+1);
6+
7+
PriorityQueue<String> priorityQueue = new PriorityQueue<>(k, new Comparator<String>() {
8+
9+
public int compare(String o1, String o2) {
10+
return countMap.get(o1).equals(countMap.get(o2))? o2.compareTo(o1):countMap.get(o1)-countMap.get(o2);
11+
}
12+
});
13+
for (String word : countMap.keySet()) {
14+
priorityQueue.offer(word);
15+
if (priorityQueue.size() > k) {
16+
priorityQueue.poll();
17+
}
18+
}
19+
List<String> ans = new ArrayList<>();
20+
while (!priorityQueue.isEmpty()) ans.add(priorityQueue.poll());
21+
Collections.reverse(ans);
22+
return ans;
23+
}
24+
}

0 commit comments

Comments
 (0)