|
1 | 1 | E |
| 2 | +1525667621 |
| 3 | +tags: Stack, Design |
2 | 4 |
|
3 | | -两个Queue,交互倒水 |
4 | | -用一个Temp做swap |
| 5 | +如题. |
5 | 6 |
|
6 | | -做法1: |
7 | | -逻辑在top()/pop()里, 每次换水,查看末尾项. |
| 7 | +#### Queue, 倒水 |
| 8 | +- 两个Queue,交互倒水 |
| 9 | +- 用一个Temp做swap |
| 10 | + |
| 11 | +##### 做法1 |
| 12 | +- 逻辑在push里面: |
| 13 | +- 1. x 放q2。 |
| 14 | +- 2. q1全部offer/append到q2. |
| 15 | +- 3. 用一个Temp做swap q1, q2. |
| 16 | +- q1的头,就一直是最后加进去的值. |
| 17 | + |
| 18 | + |
| 19 | +##### 做法2 |
| 20 | +- 逻辑在top()/pop()里, 每次换水,查看末尾项. |
8 | 21 |
|
9 | | -做法2: |
10 | | -逻辑在push里面: |
11 | | -1. x 放q2。 |
12 | | -2. q1全部offer/append到q2. |
13 | | -3. 用一个Temp做swap q1, q2. |
14 | | -q1的头,就一直是最后加进去的值. |
15 | 22 |
|
16 | 23 | ``` |
17 | 24 | /* |
|
22 | 29 | pop() -- Removes the element on top of the stack. |
23 | 30 | top() -- Get the top element. |
24 | 31 | empty() -- Return whether the stack is empty. |
| 32 | +
|
25 | 33 | Notes: |
26 | | -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. |
27 | | -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. |
28 | | -You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). |
| 34 | +You must use only standard operations of a queue -- |
| 35 | +which means only push to back, peek/pop from front, size, and is empty operations are valid. |
| 36 | +
|
| 37 | +Depending on your language, queue may not be supported natively. |
| 38 | +You may simulate a queue by using a list or deque (double-ended queue), |
| 39 | +as long as you use only standard operations of a queue. |
| 40 | +
|
| 41 | +You may assume that all operations are valid |
| 42 | +(for example, no pop or top operations will be called on an empty stack). |
29 | 43 | Credits: |
30 | 44 | Special thanks to @jianchao.li.fighter for adding this problem and all test cases. |
31 | 45 | */ |
| 46 | + |
| 47 | +class MyStack { |
| 48 | + Queue<Integer> queue; |
| 49 | + Queue<Integer> tempQueue; |
| 50 | + /** Initialize your data structure here. */ |
| 51 | + public MyStack() { |
| 52 | + queue = new LinkedList<>(); |
| 53 | + tempQueue = new LinkedList<>(); |
| 54 | + } |
| 55 | + |
| 56 | + /** Push element x onto stack. */ |
| 57 | + public void push(int x) { |
| 58 | + tempQueue = queue; |
| 59 | + queue = new LinkedList<>(); |
| 60 | + queue.offer(x); |
| 61 | + while (!tempQueue.isEmpty()) { |
| 62 | + queue.offer(tempQueue.poll()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** Removes the element on top of the stack and returns that element. */ |
| 67 | + public int pop() { |
| 68 | + return queue.poll(); |
| 69 | + } |
| 70 | + |
| 71 | + /** Get the top element. */ |
| 72 | + public int top() { |
| 73 | + return queue.peek(); |
| 74 | + } |
| 75 | + |
| 76 | + /** Returns whether the stack is empty. */ |
| 77 | + public boolean empty() { |
| 78 | + return queue.isEmpty(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
32 | 83 | /* |
33 | 84 | Thoughts: |
34 | | -1. When top()/pop() on the queue, we need to consume all the items on that queue first, then return the last item. |
| 85 | +1. When top()/pop() on the queue, we need to consume all the items on that queue first, |
| 86 | +then return the last item. |
35 | 87 | 2. Need to save the consumed items back to the queue. |
36 | 88 | */ |
37 | 89 | class MyStack { |
|
0 commit comments