Skip to content

Commit b43e991

Browse files
author
zhangruihao.zhang
committed
week_02
1 parent 984071b commit b43e991

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

Week_02/id_108/LeetCode_242_108.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.HashMap;
2+
3+
/**
4+
* @author zhangruihao.zhang
5+
* @version v1.0.0
6+
* @since 2019/04/28
7+
*/
8+
public class LeetCode_242_108 {
9+
class Solution1 {
10+
public boolean isAnagram(String s, String t) {
11+
if(s == null || t == null || s.length() != t.length()){
12+
return false;
13+
}
14+
HashMap<Character,Integer> source = new HashMap<Character,Integer>();
15+
HashMap<Character,Integer> target = new HashMap<Character,Integer>();
16+
Integer count = null;
17+
Character c = null;
18+
for(int i=0 ; i< s.length() ; i ++){
19+
c = s.charAt(i);
20+
if(source.containsKey(c)){
21+
count = source.get(c);
22+
source.put(c,count + 1);
23+
}else{
24+
source.put(c,1);
25+
}
26+
}
27+
28+
for(int i=0 ; i< t.length() ; i ++){
29+
c = t.charAt(i);
30+
if(source.containsKey(c) && source.get(c) > 0){
31+
count = source.get(c);
32+
source.put(c,count - 1);
33+
}else{
34+
target.put(c,1);
35+
}
36+
}
37+
38+
return target.keySet().size() == 0;
39+
}
40+
}
41+
42+
class Solution2 {
43+
public boolean isAnagram(String s, String t) {
44+
if(s == null || t == null || s.length() != t.length()){
45+
return false;
46+
}
47+
if(s.equals("") && t.equals("")){
48+
return true;
49+
}
50+
int[] arr = new int[26];
51+
for(int i = 0; i<s.length() ; i++){
52+
arr[s.charAt(i) - 'a'] ++;
53+
arr[t.charAt(i) - 'a'] --;
54+
}
55+
for(int i = 0;i<arr.length; i++){
56+
if(arr[i] != 0){
57+
return false;
58+
}
59+
}
60+
61+
return true;
62+
}
63+
}
64+
65+
66+
}

Week_02/id_108/LeetCode_609_108.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.HashMap;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* @author zhangruihao.zhang
8+
* @version v1.0.0
9+
* @since 2019/04/28
10+
*/
11+
public class LeetCode_609_108 {
12+
class Solution {
13+
public List<List<String>> findDuplicate(String[] paths) {
14+
HashMap<String,List<String>> contextIndexMap = new HashMap<>();
15+
List<List<String>> duplicatePaths = new LinkedList<>();
16+
for (String path : paths) {
17+
HashMap<String, List<String>> subContextIndexMap = contentIndex(path);
18+
for (Map.Entry<String, List<String>> entry : subContextIndexMap.entrySet()) {
19+
String key = entry.getKey();
20+
List<String> value = entry.getValue();
21+
List<String> addrs = contextIndexMap.get(key);
22+
if(!contextIndexMap.keySet().contains(key)){
23+
addrs = new LinkedList<>();
24+
}
25+
addrs.addAll(value);
26+
contextIndexMap.put(key, addrs);
27+
}
28+
}
29+
30+
for (Map.Entry<String, List<String>> entry : contextIndexMap.entrySet()) {
31+
if(entry.getValue().size()>1){
32+
duplicatePaths.add(entry.getValue());
33+
}
34+
}
35+
return duplicatePaths;
36+
}
37+
38+
private HashMap<String,List<String>> contentIndex(String value){
39+
HashMap<String,List<String>> map = new HashMap<>();
40+
String[] fileDir = value.split("\\s");
41+
for (int i = 1; i < fileDir.length; i++) {
42+
String text = fileDir[i];
43+
String content = text.substring(text.indexOf("(") + 1,text.indexOf(")"));
44+
String fileAddr = fileDir[0] + "/" + text.substring(0,text.indexOf("("));
45+
List<String> addrs = map.get(content);
46+
if(!map.containsKey(content)){
47+
addrs = new LinkedList<>();
48+
}
49+
addrs.add(fileAddr);
50+
map.put(content,addrs);
51+
}
52+
return map;
53+
}
54+
}
55+
}

Week_02/id_108/LeetCode_692_108.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
3+
/**
4+
* @author zhangruihao.zhang
5+
* @version v1.0.0
6+
* @since 2019/04/28
7+
*/
8+
public class LeetCode_692_108 {
9+
class Solution {
10+
public List<String> topKFrequent(String[] words, int k) {
11+
if(words == null || words.length == 0 ){
12+
return new ArrayList<>();
13+
}
14+
HashMap<String,Integer> map = new HashMap<String,Integer>();
15+
Integer count = null;
16+
for(int i=0; i<words.length; i++){
17+
count = map.get(words[i]);
18+
if(map.containsKey(words[i])){
19+
map.put(words[i],count + 1);
20+
}else{
21+
map.put(words[i],1);
22+
}
23+
}
24+
PriorityQueue<String> priorityQueue = new PriorityQueue(k,
25+
(Comparator<String>) (o1, o2) -> map.get(o1) == map.get(o2) ? o1.compareTo(o2) : - map.get(o1).compareTo(map.get(o2)));
26+
27+
map.keySet().forEach(priorityQueue::offer);
28+
29+
List<String> result = new LinkedList<>();
30+
31+
while ((k--)!=0){
32+
String peek = priorityQueue.poll();
33+
result.add(peek);
34+
}
35+
return result;
36+
}
37+
38+
39+
}
40+
}

0 commit comments

Comments
 (0)