From 3df5579ab3bb78d32766b5db4a867e9fe942b9a5 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 14 Jan 2024 14:21:17 +0800 Subject: [PATCH] =?UTF-8?q?2024-1=E6=9C=88=2013=2014=E6=97=A5=E5=A4=8D?= =?UTF-8?q?=E5=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Stack/LeetCode347.java | 2 +- .../redo/{ => redo24_1_7}/LeetCode15.java | 2 +- .../redo/{ => redo24_1_7}/LeetCode151.java | 2 +- .../redo/{ => redo24_1_7}/LeetCode18.java | 2 +- .../redo/{ => redo24_1_7}/LeetCode202.java | 2 +- .../LeetCode203_false.java} | 4 +- .../LeetCode206_false.java} | 4 +- .../LeetCode209_false.java} | 4 +- .../LeetCode2487_false.java} | 4 +- .../LeetCode287_false.java} | 4 +- .../LeetCode28_false.java} | 4 +- .../redo/redo_24_1_13/LeetCode203.java | 10 ++ .../redo/redo_24_1_13/LeetCode209.java | 46 ++++++++ .../redo/redo_24_1_13/LeetCode225.java | 73 ++++++++++++ .../redo/redo_24_1_13/LeetCode232.java | 70 +++++++++++ .../redo/redo_24_1_13/LeetCode239_false.java | 70 +++++++++++ .../redo/redo_24_1_13/LeetCode2487.java | 59 ++++++++++ .../redo/redo_24_1_13/LeetCode28.java | 33 ++++++ .../redo/redo_24_1_13/LeetCode287.java | 60 ++++++++++ .../redo/redo_24_1_13/LeetCode347.java | 110 ++++++++++++++++++ 20 files changed, 548 insertions(+), 17 deletions(-) rename src/main/java/cn/whaifree/redo/{ => redo24_1_7}/LeetCode15.java (97%) rename src/main/java/cn/whaifree/redo/{ => redo24_1_7}/LeetCode151.java (98%) rename src/main/java/cn/whaifree/redo/{ => redo24_1_7}/LeetCode18.java (98%) rename src/main/java/cn/whaifree/redo/{ => redo24_1_7}/LeetCode202.java (95%) rename src/main/java/cn/whaifree/redo/{LeetCode203.java => redo24_1_7/LeetCode203_false.java} (93%) rename src/main/java/cn/whaifree/redo/{LeetCode206.java => redo24_1_7/LeetCode206_false.java} (91%) rename src/main/java/cn/whaifree/redo/{LeetCode209.java => redo24_1_7/LeetCode209_false.java} (91%) rename src/main/java/cn/whaifree/redo/{LeetCode2487.java => redo24_1_7/LeetCode2487_false.java} (95%) rename src/main/java/cn/whaifree/redo/{LeetCode287.java => redo24_1_7/LeetCode287_false.java} (94%) rename src/main/java/cn/whaifree/redo/{LeetCode28.java => redo24_1_7/LeetCode28_false.java} (89%) create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode203.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode209.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode225.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode232.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode239_false.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode2487.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode28.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode287.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode347.java diff --git a/src/main/java/cn/whaifree/leetCode/Stack/LeetCode347.java b/src/main/java/cn/whaifree/leetCode/Stack/LeetCode347.java index a23572d..a491e87 100644 --- a/src/main/java/cn/whaifree/leetCode/Stack/LeetCode347.java +++ b/src/main/java/cn/whaifree/leetCode/Stack/LeetCode347.java @@ -48,7 +48,7 @@ public class LeetCode347 { System.out.println(i); } - + } class Solution { diff --git a/src/main/java/cn/whaifree/redo/LeetCode15.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode15.java similarity index 97% rename from src/main/java/cn/whaifree/redo/LeetCode15.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode15.java index 4bab475..819a35e 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode15.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode15.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/LeetCode151.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode151.java similarity index 98% rename from src/main/java/cn/whaifree/redo/LeetCode151.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode151.java index b8fb877..9b7f444 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode151.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode151.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/LeetCode18.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode18.java similarity index 98% rename from src/main/java/cn/whaifree/redo/LeetCode18.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode18.java index bfff447..88932d3 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode18.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode18.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/LeetCode202.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode202.java similarity index 95% rename from src/main/java/cn/whaifree/redo/LeetCode202.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode202.java index 99694c6..335742a 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode202.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode202.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/LeetCode203.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode203_false.java similarity index 93% rename from src/main/java/cn/whaifree/redo/LeetCode203.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode203_false.java index df615f7..cd13648 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode203.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode203_false.java @@ -1,10 +1,10 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import cn.whaifree.leetCode.model.ListNode; import org.junit.Test; -public class LeetCode203 { +public class LeetCode203_false { @Test public void test(){ diff --git a/src/main/java/cn/whaifree/redo/LeetCode206.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode206_false.java similarity index 91% rename from src/main/java/cn/whaifree/redo/LeetCode206.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode206_false.java index a3ca80c..c41bc00 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode206.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode206_false.java @@ -1,10 +1,10 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import cn.whaifree.leetCode.model.ListNode; import org.junit.Test; // 递归逆转链表 -public class LeetCode206 { +public class LeetCode206_false { @Test public void test() { diff --git a/src/main/java/cn/whaifree/redo/LeetCode209.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode209_false.java similarity index 91% rename from src/main/java/cn/whaifree/redo/LeetCode209.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode209_false.java index 9e3c6ca..8bd8503 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode209.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode209_false.java @@ -1,8 +1,8 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import org.junit.Test; -public class LeetCode209 { +public class LeetCode209_false { @Test public void test() { diff --git a/src/main/java/cn/whaifree/redo/LeetCode2487.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode2487_false.java similarity index 95% rename from src/main/java/cn/whaifree/redo/LeetCode2487.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode2487_false.java index 7fded65..fec578f 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode2487.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode2487_false.java @@ -1,9 +1,9 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import cn.whaifree.leetCode.model.ListNode; import org.junit.Test; -public class LeetCode2487 { +public class LeetCode2487_false { @Test public void test() { diff --git a/src/main/java/cn/whaifree/redo/LeetCode287.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode287_false.java similarity index 94% rename from src/main/java/cn/whaifree/redo/LeetCode287.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode287_false.java index 61df1fd..1562743 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode287.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode287_false.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import cn.hutool.core.lang.hash.Hash; import org.junit.Test; @@ -6,7 +6,7 @@ import org.junit.Test; import java.util.HashSet; import java.util.Set; -public class LeetCode287 { +public class LeetCode287_false { @Test public void test() { diff --git a/src/main/java/cn/whaifree/redo/LeetCode28.java b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode28_false.java similarity index 89% rename from src/main/java/cn/whaifree/redo/LeetCode28.java rename to src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode28_false.java index e59c8a9..9848359 100644 --- a/src/main/java/cn/whaifree/redo/LeetCode28.java +++ b/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode28_false.java @@ -1,8 +1,8 @@ -package cn.whaifree.redo; +package cn.whaifree.redo.redo24_1_7; import org.junit.Test; -public class LeetCode28 { +public class LeetCode28_false { @Test public void test() { diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode203.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode203.java new file mode 100644 index 0000000..fa46a08 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode203.java @@ -0,0 +1,10 @@ +package cn.whaifree.redo.redo_24_1_13; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/14 14:20 + * @注释 + */ +public class LeetCode203 { +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode209.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode209.java new file mode 100644 index 0000000..7f4040c --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode209.java @@ -0,0 +1,46 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/13 16:25 + * @注释 + */ +public class LeetCode209 { + + @Test + public void test() { + System.out.println(new Solution().minSubArrayLen(41, new int[]{1,1,1,4,4})); + } + + class Solution { + public int minSubArrayLen(int target, int[] nums) { + + int left = 0; + int right = 0; + int minLength = Integer.MAX_VALUE; + + int sum = 0; + do { + if (sum + nums[right] >= target) { + if (right - left + 1 < minLength) { + minLength = right - left + 1; + } + sum -= nums[left]; + left++; + } else { + sum += nums[right]; + right++; + } + + } while (right < nums.length); // 让右指针移动就好了 + + return minLength == Integer.MAX_VALUE ? 0 : minLength; + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode225.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode225.java new file mode 100644 index 0000000..45025d4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode225.java @@ -0,0 +1,73 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/13 13:36 + * @注释 + */ +public class LeetCode225 { + + @Test + public void test() { + MyStack myStack = new MyStack(); + myStack.push(1); + myStack.push(2); + myStack.push(3); + myStack.push(4); + myStack.push(5); + myStack.push(6); + myStack.push(7); + myStack.push(8); + myStack.push(9); + myStack.push(10); + System.out.println(myStack.pop()); + System.out.println(myStack.top()); + System.out.println(myStack.pop()); + + + } + + + class MyStack { + + public Deque queue = null; + public MyStack() { + queue = new LinkedList(); + } + + public void push(int x) { + int size = queue.size(); + queue.add(x); + for (int i = 0; i < size; i++) { + queue.add(queue.pop()); + } + } + + public int pop() { + return queue.pop(); + } + + public int top() { + return queue.peek(); + } + + public boolean empty() { + return queue.isEmpty(); + } + } + +/** + * Your MyStack object will be instantiated and called as such: + * MyStack obj = new MyStack(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.top(); + * boolean param_4 = obj.empty(); + */ +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode232.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode232.java new file mode 100644 index 0000000..4c12cb2 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode232.java @@ -0,0 +1,70 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/13 13:48 + * @注释 + */ +public class LeetCode232 { + + @Test + public void test() { + MyQueue myQueue = new MyQueue(); + myQueue.push(1); + myQueue.push(2); + myQueue.push(3); + System.out.println(myQueue.peek()); + System.out.println(myQueue.pop()); + System.out.println(myQueue.peek()); + } + + + class MyQueue { + + Deque stack1; + Deque stack2; + + public MyQueue() { + stack1 = new LinkedList<>(); + stack2 = new LinkedList<>(); + } + + public void push(int x) { + // 全部弹出到另一个栈,再全部弹回去 + while (!stack1.isEmpty()) { + stack2.push(stack1.pop()); + } + stack1.push(x); + while (!stack2.isEmpty()) { + stack1.push(stack2.pop()); + } + } + + public int pop() { + return stack1.pop(); + } + + public int peek() { + return stack1.peek(); + } + + public boolean empty() { + return stack1.isEmpty(); + } + } + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode239_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode239_false.java new file mode 100644 index 0000000..4e955fd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode239_false.java @@ -0,0 +1,70 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/13 13:58 + * @注释 + */ +public class LeetCode239_false { + + @Test + public void test() { + Solution solution = new Solution(); + int[] nums = new int[]{1, 3, -1, -3, 5, 3, 6, 7}; + int k = 3; + int[] ints = solution.maxSlidingWindow(nums, k); + for (int anInt : ints) { + System.out.println(anInt); + } + } + + class Solution { + public int[] maxSlidingWindow(int[] nums, int k) { + + + Deque queue = new LinkedList(); + int length = nums.length; + int[] res = new int[length - k + 1]; + // 从0-k-1遍历,找到最大值 + // 存下标到res数组中 + + for (int i = 0; i < k; i++) { + + while (!queue.isEmpty() && nums[i] > nums[queue.peekLast()]) { + queue.removeLast(); + } + queue.addLast(i); + } + + // 遍历剩余的k-1 --- length-1 + // 如果找到比当前优先队列首位大的下标,就加入队列 + // 并且需要判断当前窗口是不是在正确的区间内 + + // 第一个元素必然是queue.peek对应的数值 + res[0] = nums[queue.peek()]; + for (int i = k; i < length; i++) { + while (!queue.isEmpty() && nums[i] > nums[queue.peekLast()]) { + queue.removeLast(); + } + queue.addLast(i); + + if (queue.peek() <= i - k) { + queue.removeFirst(); + } + // 注入res + Integer peek = queue.peek(); + res[i - k + 1] = nums[peek]; + + } + + return res; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode2487.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode2487.java new file mode 100644 index 0000000..05b7a7b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode2487.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redo_24_1_13; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/14 12:24 + * @注释 + */ +public class LeetCode2487 { + + @Test + public void test() { + ListNode head = ListNode.listNodeFromArray(new int[]{5, 2, 13, 1, 3, 8}); + new Solution().removeNodes(head).printList(); + } + + class Solution { + public ListNode removeNodes(ListNode head) { + // 逆转 + ListNode newHead = reverse(head); + + // 移除左边有更小的节点 + remove(newHead); + + // 再逆转 + return reverse(newHead); + } + + public void remove(ListNode head) { + ListNode index = head; + ListNode targetNode = head; + while (index.next != null) { + if (index.next.val < targetNode.val) { + index.next = index.next.next; + }else { + index = index.next; + targetNode = index; + } + } + } + + public ListNode reverse(ListNode head) { + + ListNode pre = null; + ListNode index = head; + while (index != null) { + ListNode tmp = index.next; + index.next = pre; + pre = index; + index = tmp; + } + return pre; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode28.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode28.java new file mode 100644 index 0000000..64a8aa3 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode28.java @@ -0,0 +1,33 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/14 11:26 + * @注释 + */ +public class LeetCode28 { + + @Test + public void test() { + System.out.println(new Solution().strStr("leetcode", "leeto")); + } + + class Solution { + public int strStr(String haystack, String needle) { + + int length = needle.length(); + int index = 0; + int giveLength = haystack.length(); + while (index <= giveLength - length) { + if (haystack.substring(index, index + length).equals(needle)) { + return index; + } + index++; + } + return -1; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode287.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode287.java new file mode 100644 index 0000000..7608d8c --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode287.java @@ -0,0 +1,60 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/14 12:53 + * @注释 + */ +public class LeetCode287 { + + @Test + public void test() { + System.out.println(new Solution().findDuplicate(new int[]{1, 3, 4, 2, 2})); + } + + + class Solution { + + /** + * 寻找循环链表的入口 + * 1 3 4 2 2 + * 0 1 2 3 4 + * + * 0--->1--->3--->2--->4--->2 + * + * + * + * @param nums + * @return + */ + public int findDuplicate(int[] nums) { + + // 快慢指针 + int fast = 0; + int slow = 0; + + + // 找到相遇点 + do { + fast = nums[nums[fast]]; + slow = nums[slow]; + } + while (fast != slow); + + fast =0; + while (fast != slow) { + fast = nums[fast]; + slow = nums[slow]; + } + // 快指针指向头部,两个指针同志向前,直到相遇 + + return fast; + } + + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode347.java b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode347.java new file mode 100644 index 0000000..420e29b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_13/LeetCode347.java @@ -0,0 +1,110 @@ +package cn.whaifree.redo.redo_24_1_13; + +import org.junit.Test; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/13 15:10 + * @注释 + */ +public class LeetCode347 { + + @Test + public void test() { + int[] x = new Solution1().topKFrequent(new int[]{2, 2, 1, 1, 1, 1, 1, 4, 4, 4, 4, 5, 5, 5, 3, 3, 3, 3, 3, 3}, 2); + + for (int i : x) { + System.out.println(i); + } + } + + class Solution { + + public int[] topKFrequent(int[] nums, int k) { + // 统计频率 + HashMap map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + map.put(nums[i], map.get(nums[i]) + 1); + } else { + map.put(nums[i], 1); + } + } + + // 大顶堆 + PriorityQueue priorityQueue = new PriorityQueue<>( + new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return map.get(o2) - map.get(o1); + } + } + ); + + + for (Map.Entry entry : map.entrySet()) { + priorityQueue.add(entry.getKey()); + } + + int[] ints = new int[k]; + for (int i = 0; i < k; i++) { + ints[i] = priorityQueue.poll(); + } + + return ints; + } + + } + + class Solution1 { + + public int[] topKFrequent(int[] nums, int k) { + // 统计频率 + HashMap map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + map.put(nums[i], map.get(nums[i]) + 1); + } else { + map.put(nums[i], 1); + } + } + + // 小顶堆 + PriorityQueue priorityQueue = new PriorityQueue<>( + new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return map.get(o1) - map.get(o2); + } + } + ); + + + for (Map.Entry entry : map.entrySet()) { + // 导入前k个形成初始堆 + if (priorityQueue.size() < k) { + priorityQueue.add(entry.getKey()); + } else if (map.get(entry.getKey()) > map.get(priorityQueue.peek())) { + // 如果 该元素的频率比最小的值都大,则插入,并且删除最小值 + priorityQueue.remove(); + priorityQueue.add(entry.getKey()); + } + + } + + int[] ints = new int[k]; + for (int i = 0; i < k; i++) { + ints[i] = priorityQueue.poll(); + } + + return ints; + } + + } +}