-
Notifications
You must be signed in to change notification settings - Fork 23
/
Implement Stack using Queues.java
executable file
·210 lines (174 loc) · 4.9 KB
/
Implement Stack using Queues.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
E
1525667621
tags: Stack, Design
如题.
#### Queue, 倒水
- 两个Queue,交互倒水
- 用一个Temp做swap
##### 做法1
- 逻辑在push里面:
- 1. x 放q2。
- 2. q1全部offer/append到q2.
- 3. 用一个Temp做swap q1, q2.
- q1的头,就一直是最后加进去的值.
##### 做法2
- 逻辑在top()/pop()里, 每次换水,查看末尾项.
```
/*
LeetCode:
Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue --
which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively.
You may simulate a queue by using a list or deque (double-ended queue),
as long as you use only standard operations of a queue.
You may assume that all operations are valid
(for example, no pop or top operations will be called on an empty stack).
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
*/
class MyStack {
Queue<Integer> queue;
Queue<Integer> tempQueue;
/** Initialize your data structure here. */
public MyStack() {
queue = new LinkedList<>();
tempQueue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
tempQueue = queue;
queue = new LinkedList<>();
queue.offer(x);
while (!tempQueue.isEmpty()) {
queue.offer(tempQueue.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
}
/** Get the top element. */
public int top() {
return queue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
/*
Thoughts:
1. When top()/pop() on the queue, we need to consume all the items on that queue first,
then return the last item.
2. Need to save the consumed items back to the queue.
*/
class MyStack {
private Queue<Integer> queue;
private Queue<Integer> tempQueue;
/** Initialize your data structure here. */
public MyStack() {
queue = new LinkedList<>();
tempQueue = new LinkedList<>();
}
/** Find the top and backfill queue with all consumed items */
private int findTop() {
while (queue.size() > 1) {
tempQueue.offer(queue.poll());
}
int num = queue.poll();
queue = tempQueue;
tempQueue = new LinkedList<>();
return num;
}
/** Push element x onto stack. */
public void push(int x) {
queue.offer(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return findTop();
}
/** Get the top element. */
public int top() {
int num = findTop();
queue.offer(num);
return num;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
/*
LintCode:
Implement Stack by Two Queues
Implement a stack by two queues. The queue is first in first out (FIFO).
That means you can not directly pop the last element in a queue.
Have you met this question in a real interview? Yes
Example
push(1)
pop()
push(2)
isEmpty() // return false
top() // return 2
pop()
isEmpty() // return true
Tags Expand
Stack Queue
*/
/*
Thoughts:
2 queue are like two cups. We are fliping water into/out between q1 and q2.
pop and top are fliping water.
Use p1 as the base.
*/
class Stack {
private Queue<Integer> q1 = new LinkedList<Integer>();
private Queue<Integer> q2 = new LinkedList<Integer>();
// Push a new item into the stack
public void push(int x) {
q1.offer(x);
}
// Pop the top of the stack
public void pop() {
while (q1.size() > 1) {
q2.offer(q1.poll());
}
q1.poll();
swap();
}
// Return the top of the stack
public int top() {
while (q1.size() > 1) {
q2.offer(q1.poll());
}
int rst = q1.poll();
q2.offer(rst);
swap();
return rst;
}
public void swap(){
Queue<Integer> temp = q1;
q1 = q2;
q2 = temp;
}
// Check the stack is empty or not.
public boolean isEmpty() {
return q1.isEmpty();
}
}
```