File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
src/栈相关/q232_用栈实现队列/含有最大值的队列 Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments