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..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" @@ -162,6 +162,39 @@ 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(), 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 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 diff --git "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" index b6575c5fd4..9d7c20d9b6 100644 --- "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" +++ "b/problems/0347.345円211円215円K344円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:

AltStyle によって変換されたページ (->オリジナル) /