diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode20.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode20.java index ada2896..b40d9e5 100644 --- a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode20.java +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode20.java @@ -22,6 +22,7 @@ public class LeetCode20 { } class Solution { + public boolean isValid(String s) { Deque stack = new LinkedList<>(); diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode215.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode215.java new file mode 100644 index 0000000..fc28bc5 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode215.java @@ -0,0 +1,176 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.Comparator; +import java.util.PriorityQueue; +import java.util.Random; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/3 13:35 + * @注释 + */ +public class LeetCode215 { + @Test + public void test() { + int[] nums = {1}; + int k = 1; + int result = new Solution().findKthLargest(nums, k); + System.out.println(result); + } + + class Solution { + /** + * 快排 + * @param nums + * @param k + * @return + */ + public int findKthLargest(int[] nums, int k) { + return find(nums, 0, nums.length - 1, k); + } + + public int find(int[] nums, int start, int end, int k) { + if (start > end) { + return -1; + } + int q = new Random().nextInt(end - start + 1) + start; + swap(nums, q, end); + + int base = nums[end]; + int left = start; + int right = end; + while (start < end) { + while (start < end && nums[start] <= base) { + start++; + } + while (start < end && nums[end] >= base) { + end--; + } + swap(nums, start, end); + } + swap(nums, start, right); + if (start == nums.length - k) { + return nums[start]; + } else if (start > nums.length - k) { + // 左找 + return find(nums, left, start - 1, k); + } else { + // 右找 + return find(nums, start + 1, right, k); + } + } + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + + } + @Test + public void test1() { + int[] nums = {3,2,1,5,6,4}; + int k = 2; + int result = new Solution1().findKthLargest(nums, k); + System.out.println(result); + } + + class Solution1 { + public int findKthLargest(int[] nums, int k) { + PriorityQueue priorityQueue = new PriorityQueue<>( new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + + for (int num : nums) { + priorityQueue.offer(num); + if (priorityQueue.size() > k) { + priorityQueue.poll(); + } + } + return priorityQueue.peek(); + } + + } + + + class Solution2 { + + /** + * 构建大根堆 + * 把根移动到最后 + * 移动k次,顶部就是了 + */ + static class Heap{ + int[] heapArray = null; + public Heap(int[] heapArray) { + this.heapArray = heapArray; + buildMaxHeap(heapArray.length); + } + + public void buildMaxHeap(int rightEdge) { + // 遍历所有非叶子,让他们做下沉 + for (int i = rightEdge / 2 - 1; i >= 0; i--) { + build(i, rightEdge); + } + } + + /** + * + * @param index 从index左右节点向上查找 + * @param rightEdge 堆的有边界,开区间 + */ + public void build(int index, int rightEdge) { + int leftSon = index * 2 + 1; + int rightSon = index * 2 + 2; + int large = index; + if (leftSon < rightEdge && heapArray[large] < heapArray[leftSon]) { + large = leftSon; + } + if (rightSon < rightEdge && heapArray[large] < heapArray[rightSon]) { + large = rightSon; + } + + if (index != large) { + swap(index, large); + build(large, rightEdge); // large换完后large已经是小的了,小的下沉到合适的位置 + } + } + + + public void swap(int i, int j) { + int temp = heapArray[i]; + heapArray[i] = heapArray[j]; + heapArray[j] = temp; + } + + } + + + + public int findKthLargest(int[] nums, int k) { + Heap heap = new Heap(nums); + int right = nums.length; + // 构建出了最大堆 + for (int j = nums.length - 1; j >= nums.length - k + 1; j--) { + heap.swap(0, j); + heap.build(0, --right); + } + return heap.heapArray[0]; + } + + } + + @Test + public void test21() { + int[] nums = {3,2,1,5,6,4}; + int k = 2; + Solution2 solution = new Solution2(); + int i = solution.findKthLargest(nums, k); + System.out.println(i); + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode57.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode57.java new file mode 100644 index 0000000..bf64845 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode57.java @@ -0,0 +1,57 @@ +package cn.whaifree.redo.redo_all_241016; + +import java.lang.reflect.Array; +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/3 11:46 + * @注释 + */ +public class LeetCode57 { + + class Solution { + /** + * | | | | + * | | + * | | | | + * || + * | | | | + * | | + * + * | | | | + * | | + * + * @param intervals + * @param newInterval + * @return + */ + public int[][] insert(int[][] intervals, int[] newInterval) { + int[][] res = new int[intervals.length + 1][2]; + int i = 0; + int index = 0; + // 找到newInterval的左边界位置 + while (intervals[index][0] < newInterval[0]) { + res[i++] = intervals[index]; + index++; + } + // 此时index的value >= newInterval的起始位置 + + // 找到newInterval右边边界位置 + while (intervals[index][1] <= newInterval[1]) { + newInterval[0] = Math.min(newInterval[0], intervals[index][0]); + newInterval[1] = Math.max(newInterval[1], intervals[index][1]); + index ++; + } + + res[i++] = newInterval; + // 把newInterval右边界右边的位置全部移动过来 + while (index < intervals.length) { + res[i++] = intervals[index]; + index++; + } + return Arrays.copyOf(res, i); + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java index 53a4617..19e943b 100644 --- a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java @@ -9,6 +9,7 @@ import org.junit.Test; * @注释 */ public class LeetCode69 { + @Test public void test() { Solution1 solution = new Solution1(); diff --git a/pom.xml b/pom.xml index 0992f3f..b9e49f4 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,8 @@ ForJdk8 ForJdk17 SeleniumDemo + SpringCloud + SpringDemo