[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 standard operations of a queue -- which means only push to backpeek/pop from frontsize, 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).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Solution:

使用两个队列qm,qs, qm存储元素,qs作为临时存储:

1)入栈列时直接qm.push

2)出栈时,先将qm中的前N-1个元素push到qs中,然后pop出最后的第N个元素,之后把qs中的元素转移到qm中

 1 class Stack {
 2 public:
 3     // Push element x onto stack.
 4     void push(int x)
 5     {
 6         qm.push(x);
 7     }
 8
 9     // Removes the element on top of the stack.
10     void pop()
11     {
12         while (qm.size() > 1)
13         {
14             qs.push(qm.front());
15             qm.pop();
16         }
17
18         qm.pop();
19
20         while (!qs.empty())
21         {
22             qm.push(qs.front());
23             qs.pop();
24         }
25     }
26
27     // Get the top element.
28     int top()
29     {
30         return qm.back();
31     }
32
33     // Return whether the stack is empty.
34     bool empty()
35     {
36         return qm.empty();
37     }
38
39 private:
40     std::queue<int> qm;
41     std::queue<int> qs;
42 };
时间: 2024-10-26 19:34:54

[leetcode] 225. Implement Stack using Queues的相关文章

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

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

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