Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 38f972b

Browse files
update
1 parent 4b762c3 commit 38f972b

File tree

1 file changed

+34
-0
lines changed
  • src/栈相关/q232_用栈实现队列/含有最大值的队列

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package 栈相关.q232_用栈实现队列.含有最大值的队列;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class MaxQueue {
7+
8+
private Queue<Integer> queue;
9+
private LinkedList<Integer> max;
10+
11+
public MaxQueue() {
12+
queue = new LinkedList<>();
13+
max = new LinkedList<>();
14+
}
15+
16+
public int max_value() {
17+
return max.size() == 0 ? -1 : max.getFirst();
18+
}
19+
20+
public void push_back(int value) {
21+
queue.add(value);
22+
while (max.size() != 0 && max.getLast() < value) {
23+
max.removeLast();
24+
}
25+
max.add(value);
26+
}
27+
28+
public int pop_front() {
29+
if (max.size() != 0 && queue.peek().equals(max.getFirst())) {
30+
max.removeFirst();
31+
}
32+
return queue.size() == 0 ? -1 : queue.poll();
33+
}
34+
}

0 commit comments

Comments
(0)

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