Skip to content

Commit 6dda9c6

Browse files
author
chenweijie
committed
stack
1 parent 29af5be commit 6dda9c6

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

src/main/java/com/chen/algorithm/study/test703/ObjectPriorityQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public int add(int num) {
2626

2727
if (queue.size() < limit) {
2828
queue.offer(num);
29-
} else if (queue.peek() > num) {
29+
} else if (queue.peek() < num) {
3030
queue.poll();
3131
queue.offer(num);
3232
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.chen.algorithm.znn.stack.test225;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* @Auther: zhunn
8+
* @Date: 2020/10/26 22:20
9+
* @Description: 用两个队列实现一个栈, 官方解法
10+
*/
11+
public class MyStack {
12+
13+
private Queue<Integer> q1;
14+
private Queue<Integer> q2;
15+
16+
public MyStack() {
17+
q1 = new LinkedList<>();
18+
q2 = new LinkedList<>();
19+
}
20+
21+
public void push(int value) {
22+
q2.offer(value);
23+
while (!q1.isEmpty()) {
24+
q2.offer(q1.poll());
25+
}
26+
27+
Queue<Integer> temp = q1;
28+
q1 = q2;
29+
q2 = temp;
30+
}
31+
32+
public int pop() {
33+
return q1.poll();
34+
}
35+
36+
public int top() {
37+
return q1.peek();
38+
}
39+
40+
public boolean empty() {
41+
return q1.isEmpty();
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.chen.algorithm.znn.stack.test225;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* @Auther: zhunn
8+
* @Date: 2020/10/26 21:20
9+
* @Description: 用两个队列实现一个栈
10+
*/
11+
public class Solution {
12+
13+
private Queue<Integer> q1 = new LinkedList<>();
14+
private Queue<Integer> q2 = new LinkedList<>();
15+
private int top;
16+
17+
public void push(Integer value) {
18+
q1.add(value);
19+
top = value;
20+
}
21+
22+
public int pop() {
23+
while (q1.size() > 1) {
24+
q2.add(q1.peek());
25+
top = q1.remove();
26+
}
27+
int result = q1.remove();
28+
Queue<Integer> temp = q1;
29+
q1 = q2;
30+
q2 = temp;
31+
return result;
32+
}
33+
34+
public int top() {
35+
return top;
36+
}
37+
38+
public boolean empty() {
39+
return q1.isEmpty() && q2.isEmpty();
40+
}
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.chen.algorithm.znn.stack.test703;
2+
3+
import org.junit.Test;
4+
5+
import java.util.PriorityQueue;
6+
7+
/**
8+
* @Auther: zhunn
9+
* @Date: 2020/10/26 16:55
10+
* @Description: 求数组中的第K个最大元素:1-暴力;2-优先级队列
11+
*/
12+
public class Solution {
13+
14+
class KthLargest {
15+
private PriorityQueue<Integer> queue;
16+
private int limit;
17+
18+
public KthLargest(int size, int[] nums) {
19+
this.limit = size;
20+
queue = new PriorityQueue<>(limit);
21+
for (int num : nums) {
22+
add(num);
23+
}
24+
}
25+
26+
public int add(int num) {
27+
28+
if (queue.size() < limit) {
29+
queue.offer(num);
30+
} else if (queue.peek() < num) {
31+
queue.poll();
32+
queue.offer(num);
33+
}
34+
35+
return queue.peek();
36+
}
37+
}
38+
39+
40+
@Test
41+
public void testCase() {
42+
int[] n = {3, 2, 3, 1, 2, 4, 5, 5, 6};
43+
KthLargest kthLargest = new KthLargest(2, n);
44+
System.out.println(kthLargest.add(3));
45+
46+
}
47+
}

0 commit comments

Comments
 (0)