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

题目意思:用队列实现栈,push(),pop(),top(),empty()

思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个

  ps:用栈实现队列,参考剑指offer

 1 class Stack {
 2 private:
 3     queue<int> q[2];
 4     int flag=0;
 5 public:
 6     // Push element x onto stack.
 7     void push(int x) {
 8         q[flag].push(x);
 9     }
10
11     // Removes the element on top of the stack.
12     void pop() {
13         while(q[flag].size()>1){
14             q[1-flag].push(q[flag].front());
15             q[flag].pop();
16         }
17         q[flag].pop();
18         flag=1-flag;
19     }
20
21     // Get the top element.
22     int top() {
23         return q[flag].back();
24     }
25
26     // Return whether the stack is empty.
27     bool empty() {
28         if(q[flag].empty()){
29             return true;
30         }
31         return false;
32     }
33 };
时间: 2024-08-08 09:30:11

225 Implement Stack using Queues(用队列实现栈)的相关文章

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

leetcode_225题——Implement Stack using Queues (队列,栈)

Implement Stack using Queues Total Accepted: 9609 Total Submissions: 31710My Submissions Question Solution Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the st

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. 解题思路: 用队列实现栈的操作.包括输入.输出.取栈

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 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

(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

[leedcode 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