From 0146595da4731eeda402b7b066669ab6eeb03aea Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 6 May 2025 15:50:59 -0700 Subject: [PATCH 1/5] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC=20Java=20Version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java Solution with SwitchCase Approach 添加 Java 代码 Switch Case。更简洁一些。思路一样 都是 Stack --- ...76\345\274\217\346\261\202\345\200\274.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 6d21452d1d..f73438dcc9 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -162,6 +162,40 @@ class Solution { } ``` +```Java +// Switch Case 写法,更简洁一些 +class Solution { + public int evalRPN(String[] tokens) { + Stack stk = new Stack<>(); + for (String token : tokens) { + + // 运算符Operator + - * / + if ("+-*/".contains(token)) { + int a = stk.pop(); + int b = stk.pop(); + switch (token) { + case "+": + stk.push(a + b); + break; + case "-": + stk.push(b - a); + break; + case "*": + stk.push(a * b); + break; + case "/": + stk.push(b / a); + break; + } + } else { + stk.push(Integer.parseInt(token)); + } + } + return stk.pop(); + } +} +``` + ### Python3: ```python From 703a500a8ee743972eeef234e882be79e1857cf0 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 6 May 2025 15:55:48 -0700 Subject: [PATCH 2/5] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index f73438dcc9..ab7accd56c 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -171,7 +171,7 @@ class Solution { // 运算符Operator + - * / if ("+-*/".contains(token)) { - int a = stk.pop(); + int a = stk.pop(); int b = stk.pop(); switch (token) { case "+": From 609ac9b7212a7e96d026d038d13367376cdf446f Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 6 May 2025 17:00:03 -0700 Subject: [PATCH 3/5] =?UTF-8?q?Update=200239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC=20Java=20Solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java Solution with Priority Queue Solution. --- ...43\346\234\200\345\244\247\345\200\274.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 875f1bd193..194eff8492 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -260,7 +260,9 @@ class Solution { return res; } } +``` +```Java //解法二 //利用双端队列手动实现单调队列 /** @@ -296,6 +298,39 @@ class Solution { } ``` +```Java +//解法三 +//用优先队列 Priority Queue, 时间Time: O(nlogk) | 空间Space: O(k) +/** +* 很好理解,PQ 会优先置顶最大值(Java Comparator) +* 维护window里的 PQ, PQ内部记录 pair +**/ +class Solution { + public int[] maxSlidingWindow(int[] nums, int k) { + if (nums == null || k <= 0) + return new int[0]; + + int n = nums.length; + int[] res = new int[n - k + 1]; + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b[0] - a[0]); + + for (int i = 0; i < n; i++) { + maxHeap.offer(new int[] { nums[i], i }); + + // 删除窗口之外的元素 + while (maxHeap.peek()[1] <= i - k) { + maxHeap.poll(); + } + + if (i >= k - 1) { + res[i - k + 1] = maxHeap.peek()[0]; + } + } + return res; + } +} +``` + ### Python: #### 解法一:使用自定义的单调队列类 ```python From 511c415b750cbb8f1fddd181856837756e4422a2 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 6 May 2025 17:02:30 -0700 Subject: [PATCH 4/5] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC=20Java=20Solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update the formate --- ...241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index ab7accd56c..796b475371 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -171,8 +171,7 @@ class Solution { // 运算符Operator + - * / if ("+-*/".contains(token)) { - int a = stk.pop(); - int b = stk.pop(); + int a = stk.pop(), b = stk.pop(); switch (token) { case "+": stk.push(a + b); From 116a2da4f05b28465deab3058c202d994b3c742f Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 6 May 2025 18:01:21 -0700 Subject: [PATCH 5/5] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA=E9=AB=98?= =?UTF-8?q?=E9=A2=91=E5=85=83=E7=B4=A0=20Java=20Solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java solution with Bucket Sort. --- ...30\351\242\221\345\205\203\347\264\240.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index b6575c5fd4..9d7c20d9b6 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -213,6 +213,49 @@ class Solution { return res; } } +``` +Java解法三:Bucket Sort 桶排序 +```java +/** +* 利用 桶排序 +* 时间复杂度 O(n) +* 空间复杂度 O(n) +**/ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + + Map freqMap = new HashMap<>(); + for (int num : nums) { + freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); + } + + List[] bucket = new List[nums.length + 1]; + for (int i = 0; i <= nums.length; i++) { + bucket[i] = new ArrayList<>(); + } + + for (Map.Entry entry : freqMap.entrySet()) { + int num = entry.getKey(); + int freq = entry.getValue(); + bucket[freq].add(num); + } + + List result = new ArrayList<>(); + for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { + if (!bucket[i].isEmpty()) { + result.addAll(bucket[i]); + } + } + + int[] res = new int[k]; + for (int i = 0; i < k; i++) { + res[i] = result.get(i); + } + + return res; + } +} + ``` ### Python: