Skip to content

Commit 90ec6fd

Browse files
committed
补充
1 parent af0506b commit 90ec6fd

11 files changed

Lines changed: 407 additions & 28 deletions

File tree

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,85 @@
11
package com.chen.algorithm.znn.array.offer.test57;
22

3+
import com.alibaba.fastjson.JSON;
4+
import org.junit.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
39
/**
10+
* https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/solution/shi-yao-shi-hua-dong-chuang-kou-yi-ji-ru-he-yong-h/
11+
*
412
* @Auther: zhunn
513
* @Date: 2020/11/08 15:25
6-
* @Description: 剑指 Offer 57 - II. 和为s的连续正数序列
14+
* @Description: 剑指 Offer 57 - II. 和为s的连续正数序列:滑动窗口法
715
*/
816
public class Solution {
17+
18+
//public int[][] findContinuousSequence(int target) {
19+
// int i = 1; // 滑动窗口的左边界
20+
// int j = 1; // 滑动窗口的右边界
21+
// int sum = 0; // 滑动窗口中数字的和
22+
// List<int[]> res = new ArrayList<>();
23+
//
24+
// while (i <= target / 2) {
25+
// if (sum < target) {
26+
// // 右边界向右移动
27+
// sum += j;
28+
// j++;
29+
// } else if (sum > target) {
30+
// // 左边界向右移动
31+
// sum -= i;
32+
// i++;
33+
// } else {
34+
// // 记录结果
35+
// int[] arr = new int[j - i];
36+
// for (int k = i; k < j; k++) {
37+
// arr[k - i] = k;
38+
// }
39+
// res.add(arr);
40+
// // 左边界向右移动
41+
// sum -= i;
42+
// i++;
43+
// }
44+
// }
45+
//
46+
// return res.toArray(new int[res.size()][]);
47+
//}
48+
49+
/**
50+
* 滑动窗口法
51+
*
52+
* @param target
53+
* @return
54+
*/
55+
public int[][] findContinuousSequence(int target) {
56+
int left = 1, right = 1;
57+
int sum = 0;
58+
List<int[]> res = new ArrayList<>();
59+
60+
while (left <= target / 2) {
61+
if (sum < target) {
62+
sum += right;
63+
right++;
64+
} else if (sum > target) {
65+
sum -= left;
66+
left++;
67+
} else {
68+
int[] arr = new int[right - left];
69+
for (int i = left; i < right; i++) {
70+
arr[i - left] = i;
71+
}
72+
res.add(arr);
73+
74+
sum -= left;
75+
left++;
76+
}
77+
}
78+
return res.toArray(new int[res.size()][]);
79+
}
80+
81+
@Test
82+
public void test() {
83+
System.out.println(JSON.toJSONString(findContinuousSequence(15)));
84+
}
985
}
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
package com.chen.algorithm.znn.array.test11;
22

3+
import org.junit.Test;
4+
35
/**
6+
* https://leetcode-cn.com/problems/container-with-most-water/solution/sheng-zui-duo-shui-de-rong-qi-by-leetcode-solution/
7+
*
48
* @Auther: zhunn
59
* @Date: 2020/11/08 15:25
6-
* @Description: 盛最多水的容器
10+
* @Description: 盛最多水的容器:双指针法
711
*/
812
public class Solution {
13+
14+
public int maxArea(int[] height) {
15+
int left = 0, right = height.length - 1;
16+
int res = 0;
17+
18+
while (left < right) {
19+
int area = Math.min(height[left], height[right]) * (right - left);
20+
res = Math.max(res, area);
21+
if (height[left] <= height[right]) {
22+
left++;
23+
} else {
24+
right--;
25+
}
26+
}
27+
return res;
28+
}
29+
30+
@Test
31+
public void test() {
32+
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
33+
System.out.println(maxArea(height));
34+
}
935
}
Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,65 @@
11
package com.chen.algorithm.znn.array.test287;
22

3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
37
/**
48
* @Auther: zhunn
59
* @Date: 2020/11/08 15:25
6-
* @Description: 寻找重复数
10+
* @Description: 寻找重复数:1-排序迭代;2-快慢指针
11+
* 将i 指向 nums[i],如果有重复,就会形成环状
712
*/
813
public class Solution {
14+
15+
/**
16+
* 1-排序迭代
17+
*
18+
* @param nums
19+
* @return
20+
*/
21+
public int findDuplicate(int[] nums) {
22+
if (nums == null || nums.length == 0) {
23+
return -1;
24+
}
25+
Arrays.sort(nums);
26+
for (int i = 1; i < nums.length; i++) {
27+
if (nums[i] == nums[i - 1]) {
28+
return nums[i];
29+
}
30+
}
31+
return -1;
32+
}
33+
34+
/**
35+
* 2-快慢指针
36+
*
37+
* @param nums
38+
* @return
39+
*/
40+
public int findDuplicate2(int[] nums) {
41+
if (nums == null || nums.length == 0) {
42+
return -1;
43+
}
44+
45+
int slow = 0, fast = 0;
46+
do {
47+
slow = nums[slow];
48+
fast = nums[nums[fast]];
49+
} while (slow != fast);
50+
51+
slow = 0;
52+
while (slow != fast) {
53+
slow = nums[slow];
54+
fast = nums[fast];
55+
}
56+
return slow;
57+
}
58+
59+
@Test
60+
public void test() {
61+
int[] nums = {1, 6, 7, 7, 7, 2, 3, 5};
62+
System.out.println(findDuplicate(nums));
63+
System.out.println(findDuplicate2(nums));
64+
}
965
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.chen.algorithm.znn.array.test674;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/solution/zui-chang-lian-xu-di-zeng-xu-lie-by-leetcode/
7+
*
8+
* @Auther: zhunn
9+
* @Date: 2020/11/08 15:25
10+
* @Description: 最长连续递增序列:滑动窗口
11+
*/
12+
public class Solution {
13+
14+
public int findLengthOfLCIS(int[] nums) {
15+
int res = 0, anchor = 0;
16+
for (int i = 0; i < nums.length; i++) {
17+
if (i > 0 && nums[i - 1] >= nums[i]) {
18+
anchor = i;
19+
}
20+
res = Math.max(res, i - anchor + 1);
21+
}
22+
return res;
23+
}
24+
25+
@Test
26+
public void test() {
27+
int[] nums = {1, 3, 5, 4, 7};
28+
int[] nums2 = {2, 2, 2, 2, 2};
29+
System.out.println(findLengthOfLCIS(nums));
30+
System.out.println(findLengthOfLCIS(nums2));
31+
}
32+
}

‎src/main/java/com/chen/algorithm/znn/heap/Test.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* @Description: 堆相关
77
* 1、堆的基本操作 heap/Heap
88
*
9-
* 2、堆化,大根堆,小根堆,堆排序,堆的应用(优先级队列,topK,中位数)
9+
* 2、堆化,大根堆,小根堆,堆排序
10+
* 3、堆的应用(大根堆-优先级队列,小根堆-topK,中位数)
1011
* java的priorityQueue 用小根堆实现的优先级队列
1112
*/
1213
public class Test {
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
11
package com.chen.algorithm.znn.heap.test347;
22

3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
37
/**
48
* @Auther: zhunn
59
* @Date: 2020/11/08 15:25
6-
* @Description: 前 K 个高频元素
10+
* @Description: 前 K 个高频元素:PriorityQueue-小根堆
711
*/
812
public class Solution {
13+
14+
public int[] topKFrequent(int[] nums, int k) {
15+
// 计算频次
16+
Map<Integer, Integer> map = new HashMap<>();
17+
for (int num : nums) {
18+
if (map.containsKey(num)) {
19+
map.put(num, map.get(num) + 1);
20+
} else {
21+
map.put(num, 1);
22+
}
23+
}
24+
25+
//PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.comparingInt(map::get));
26+
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
27+
@Override
28+
public int compare(Integer o1, Integer o2) {
29+
return map.get(o1) - map.get(o2);
30+
}
31+
});
32+
33+
for (Integer key : map.keySet()) {
34+
if (queue.size() < k) {
35+
queue.add(key);
36+
} else if (map.get(queue.peek()) < map.get(key)) {
37+
queue.poll();
38+
queue.add(key);
39+
}
40+
}
41+
42+
int[] res = new int[k];
43+
int i = 0;
44+
while (!queue.isEmpty()) {
45+
res[i] = queue.poll();
46+
i++;
47+
}
48+
return res;
49+
}
50+
51+
@Test
52+
public void test() {
53+
int[] nums = {5, 2, 5, 3, 5, 3, 1, 1, 3};
54+
int[] res = topKFrequent(nums, 2);
55+
System.out.println(Arrays.toString(res));
56+
}
957
}
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
package com.chen.algorithm.znn.stack.test496;
22

3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Stack;
9+
310
/**
11+
* https://leetcode-cn.com/problems/next-greater-element-i/solution/xia-yi-ge-geng-da-yuan-su-i-by-leetcode/
12+
*
413
* @Auther: zhunn
514
* @Date: 2020/11/08 15:25
6-
* @Description: 下一个更大元素
15+
* @Description: 下一个更大元素:单调栈
716
*/
817
public class Solution {
18+
19+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
20+
Map<Integer, Integer> map = new HashMap<>();
21+
Stack<Integer> stack = new Stack<>();
22+
23+
for (int i = 0; i < nums2.length; i++) {
24+
int num = nums2[i];
25+
while (!stack.isEmpty() && num > stack.peek()) {
26+
map.put(stack.pop(), num);
27+
}
28+
stack.push(num);
29+
}
30+
31+
while (!stack.isEmpty()) {
32+
map.put(stack.pop(), -1);
33+
}
34+
35+
int[] res = new int[nums1.length];
36+
for (int i = 0; i < nums1.length; i++) {
37+
res[i] = map.get(nums1[i]);
38+
}
39+
return res;
40+
}
41+
42+
@Test
43+
public void test() {
44+
int[] nums1 = {4, 1, 2};
45+
int[] nums2 = {1, 3, 4, 2};
46+
int[] res = nextGreaterElement(nums1, nums2);
47+
System.out.println(Arrays.toString(res));
48+
}
949
}

‎src/main/java/com/chen/algorithm/znn/stack/test674/Solution.java‎

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

0 commit comments

Comments
 (0)