Leetcode 225 Implement Stack using Queues STL

用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的

push时插入到空的队列中,然后将队列中的元素移到另一个队列中

pop时从不空的队列中pop()

peek时从不空的队列中取出front()

 1 class Stack {
 2 public:
 3     queue<int> q[2];
 4
 5     // Push element x onto stack.
 6     void move(int x){
 7         while(!q[1-x].empty()){
 8             q[x].push(q[1-x].front());
 9             q[1-x].pop();
10         }
11     }
12
13     void push(int x) {
14         for(int i = 0; i < 2; ++i){
15             if(q[i].empty()){
16                 q[i].push(x);
17                 move(i);
18                 break;
19             }
20         }
21     }
22
23     // Removes the element on top of the stack.
24     void pop() {
25         for(int i = 0; i < 2; ++i){
26             if(!q[i].empty()){
27                 q[i].pop();
28                 break;
29             }
30         }
31     }
32
33     // Get the top element.
34     int top() {
35         for(int i = 0; i < 2; ++i){
36             if(!q[i].empty()){
37                 return q[i].front();
38             }
39         }
40     }
41
42     // Return whether the stack is empty.
43     bool empty() {
44         return q[0].empty() && q[1].empty();
45     }
46 };
时间: 2024-12-30 03:10:51

Leetcode 225 Implement Stack using Queues STL的相关文章

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

[leetcode] 225. Implement Stack using Queues

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 s

[LeetCode] 225. Implement Stack using Queues Java

题目: 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 on

Java for LeetCode 225 Implement Stack using Queues

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 s

(easy)LeetCode 225.Implement Stack using Queues

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 s

leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

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 s

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

232. Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whet

[LeetCode][JavaScript]Implement Stack using Queues

Implement Stack using Queues 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 empt

【LeetCode】225 - Implement Stack using Queues

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 s