Skip to content

Commit 94f0250

Browse files
Merge pull request #390 from SeanMrLi/master
BJ001-1904108-week02作业
2 parents 1ff354f + b43e991 commit 94f0250

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

Week_01/id_108/LeetCode_153_108.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/24
5+
*/
6+
public class LeetCode_153_108 {
7+
8+
class Solution1 {
9+
public int findMin(int[] nums) {
10+
if(nums.length == 1){
11+
return nums[0];
12+
}
13+
if(nums.length == 2){
14+
return nums[0] > nums[1] ? nums[1] : nums[0];
15+
}
16+
if(nums[0] < nums[nums.length-1]){
17+
return nums[0];
18+
}
19+
int min = 0;
20+
int high = nums.length - 1;
21+
int mid = 0;
22+
while(min <= high){
23+
//针对1、2、3这种情况
24+
if(min == high){
25+
return nums[min];
26+
}
27+
mid = min + ((high-min) >> 1);
28+
if(nums[mid] > nums[high]){
29+
if(nums[mid+1] < nums[mid]){
30+
return nums[mid+1];
31+
}
32+
min = mid + 1;
33+
continue;
34+
}
35+
if(nums[mid] < nums[min]){
36+
if(nums[mid-1] > nums[mid]){
37+
return nums[mid];
38+
}
39+
high = mid - 1;
40+
}
41+
}
42+
return -1;
43+
}
44+
45+
//二分查找
46+
//分为两种情况
47+
//mid比最后一个元素大,判断它右边的值是否小于它,如果小于的话就返回右边的值,否则的话,min取mid+1
48+
//mid比第一个元素小,判断它左边的值是否比它大,如果大的话,直接返回它自己,否则的话high取mid-1
49+
}
50+
51+
class Solution2 {
52+
public int findMin(int[] nums) {
53+
if(nums.length == 1){
54+
return nums[0];
55+
}
56+
int min = 0;
57+
int high = nums.length - 1;
58+
int mid = 0;
59+
while(min < high){
60+
mid = min + ((high-min) >> 1);
61+
if(nums[mid] > nums[high]){
62+
min = mid + 1;
63+
} else {
64+
high = mid;
65+
}
66+
}
67+
return nums[min];
68+
}
69+
70+
//二分查找
71+
//分为两种情况
72+
//mid比最后一个元素大,判断它右边的值是否小于它,如果小于的话就返回右边的值,否则的话,min取mid+1
73+
//mid比第一个元素小,判断它左边的值是否比它大,如果大的话,直接返回它自己,否则的话high取mid-1
74+
75+
76+
}
77+
}

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)