Skip to content

Commit c71bbfc

Browse files
author
chenweijie
committed
大根堆和小根堆 四个数相加
1 parent 3103a09 commit c71bbfc

File tree

8 files changed

+223
-115
lines changed

8 files changed

+223
-115
lines changed

src/main/java/com/chen/CacheMap.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2020-08-26 10:29
9+
*/
10+
public class CacheMap<K, V> extends LinkedHashMap<K, V> {
11+
12+
13+
private int limit;
14+
15+
public CacheMap(int limit) {
16+
super(limit, 0.75f, true);
17+
this.limit = limit;
18+
}
19+
20+
public void putVal(K k, V v) {
21+
super.put(k, v);
22+
}
23+
24+
public V getVal(K k) {
25+
return super.getOrDefault(k, (V) new Object());
26+
}
27+
28+
29+
@Override
30+
protected boolean removeEldestEntry(Map.Entry<K, V> entry) {
31+
return super.size() > limit;
32+
}
33+
34+
35+
}

src/main/java/com/chen/Node.java

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/main/java/com/chen/algorithm/study/test121/Solution3.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/**
66
* 双指针算法
77
*
8+
* https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-l-3/
9+
*
810
* @author : chen weijie
911
* @Date: 2019-11-01 23:54
1012
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.chen.algorithm.study.test18;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* @author : chen weijie
9+
* @Date: 2020-08-30 17:15
10+
*/
11+
public class Solution {
12+
13+
14+
public List<List<Integer>> fourSum(int[] nums, int target) {
15+
16+
List<List<Integer>> res = new ArrayList<>();
17+
if (nums == null || nums.length == 0) {
18+
return res;
19+
}
20+
Arrays.sort(nums);
21+
22+
for (int i = 0; i < nums.length; i++) {
23+
if (i > 0 && nums[i - 1] == nums[i]) {
24+
continue;
25+
}
26+
27+
for (int j = i + 1; j < nums.length; j++) {
28+
29+
if (j > i + 1 && nums[j - 1] == nums[j]) {
30+
continue;
31+
}
32+
int left = j + 1, right = nums.length - 1;
33+
while (left < right) {
34+
int sum = nums[i] + nums[j] + nums[left] + nums[right];
35+
36+
if (sum > target) {
37+
right--;
38+
} else if (sum < target) {
39+
left++;
40+
} else {
41+
res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
42+
43+
while (left < right && nums[left + 1] == nums[left]) {
44+
left++;
45+
}
46+
while (left < right && nums[right] == nums[right - 1]) {
47+
right--;
48+
}
49+
left++;
50+
right--;
51+
}
52+
}
53+
}
54+
}
55+
56+
return res;
57+
}
58+
}

src/main/java/com/chen/algorithm/study/test227/Solution.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public int calculate(String s) {
5656

5757
int res = 0;
5858
while (!stack.isEmpty()) {
59-
res += stack.peek();
60-
stack.pop();
59+
res += stack.pop();
6160
}
6261

6362
return res;
@@ -66,7 +65,7 @@ public int calculate(String s) {
6665

6766
@Test
6867
public void testCase() {
69-
System.out.println(calculate("3/2"));
68+
System.out.println(calculate("-30+5/2 "));
7069
}
7170

7271

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.chen.algorithm.study.test239;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Arrays;
7+
8+
/**
9+
* https://leetcode-cn.com/problems/sliding-window-maximum/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-5-3/
10+
* @author : chen weijie
11+
* @Date: 2020-08-30 01:29
12+
*/
13+
public class Solution {
14+
15+
16+
public int[] maxSlidingWindow(int[] nums, int k) {
17+
18+
int[] res = new int[nums.length - k + 1];
19+
// 递减队列
20+
ArrayDeque<Integer> queue = new ArrayDeque<>();
21+
22+
for (int i = 0; i < nums.length; i++) {
23+
24+
// 添加滑入的数 nums[i] ,构造递减队列
25+
while (!queue.isEmpty() && queue.peekLast() < nums[i]) {
26+
queue.pollLast();
27+
}
28+
queue.addLast(nums[i]);
29+
30+
// 删除滑出的数 nums[i - k],如果删除的数等于队头,删除队头
31+
if (i >= k && nums[i - k] == queue.peekFirst()) {
32+
queue.pollFirst();
33+
}
34+
35+
// 写入当前最大值
36+
if (i >= k - 1) {
37+
res[i - k + 1] = queue.peekFirst();
38+
}
39+
}
40+
return res;
41+
}
42+
43+
@Test
44+
public void testCase() {
45+
46+
int[] nums = {1, 3, -1, -3, 5, 3, 2, 6, 7};
47+
int k = 3;
48+
System.out.println(Arrays.toString(maxSlidingWindow(nums, k)));
49+
50+
51+
}
52+
53+
54+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.chen.algorithm.study.test242;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-08-30 16:21
6+
*/
7+
public class Solution2 {
8+
9+
10+
public boolean isAnagram(String s, String t) {
11+
12+
if (s.length() != t.length()) {
13+
return false;
14+
}
15+
16+
int[] counter = new int[26];
17+
18+
for (int i = 0; i < s.length(); i++) {
19+
counter[s.charAt(i) - 'a']++;
20+
counter[t.charAt(i) - 'a']--;
21+
}
22+
23+
for (int count : counter) {
24+
if (count != 0) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.chen.dataStructure.study.queen.priorityQueue;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
/**
7+
* PriorityQueue 默认实现的是小顶堆,如果传入comparator参数定义排序,则可以实现大顶堆。
8+
*
9+
* @author : chen weijie
10+
* @Date: 2020-08-30 01:23
11+
*/
12+
public class MaxHeap {
13+
14+
private int size;
15+
16+
public PriorityQueue<Integer> queue = new PriorityQueue<>(size, new Comparator<Integer>() {
17+
@Override
18+
public int compare(Integer o1, Integer o2) {
19+
return o2 - o1;
20+
}
21+
});
22+
23+
public MaxHeap(int size, int[] nums) {
24+
this.size = size;
25+
for (int n : nums) {
26+
add(n);
27+
}
28+
}
29+
30+
31+
private Integer add(int val) {
32+
33+
if (queue.size() < size) {
34+
queue.offer(val);
35+
} else if (queue.peek() != null && queue.peek() > val) {
36+
queue.poll();
37+
queue.offer(val);
38+
}
39+
return queue.peek();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)