Skip to content

Update 150 Java Version with SwitchCase #2937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions problems/0150.逆波兰表达式求值.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,39 @@ class Solution {
}
```

```Java
// Switch Case 写法,更简洁一些
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> 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
Expand Down
35 changes: 35 additions & 0 deletions problems/0239.滑动窗口最大值.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ class Solution {
return res;
}
}
```

```Java
//解法二
//利用双端队列手动实现单调队列
/**
Expand Down Expand Up @@ -296,6 +298,39 @@ class Solution {
}
```

```Java
//解法三
//用优先队列 Priority Queue, 时间Time: O(nlogk) | 空间Space: O(k)
/**
* 很好理解,PQ 会优先置顶最大值(Java Comparator)
* 维护window里的 PQ, PQ内部记录 <value, index> 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<int[]> 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
Expand Down
43 changes: 43 additions & 0 deletions problems/0347.前K个高频元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> freqMap = new HashMap<>();
for (int num : nums) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}

List<Integer>[] bucket = new List[nums.length + 1];
for (int i = 0; i <= nums.length; i++) {
bucket[i] = new ArrayList<>();
}

for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
int num = entry.getKey();
int freq = entry.getValue();
bucket[freq].add(num);
}

List<Integer> 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:
Expand Down