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

【LeetCode】225 - Implement Stack using Queues的相关文章

【easy】225. Implement Stack using Queues

用队列实现栈.这个实现方法十分的简单,就是在push这一步的时候直接变成逆序. class MyStack { private: queue<int> q; queue<int> q2; public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { q.push(x); for(int i=0;i<q.

【leetcode?python】 225. Implement Stack using Queues

#栈是先进后出 #队列先进先出 class Stack(object):    def __init__(self):        """        initialize your data structure here.        """        self.inQueue=[]        self.outQueue=[] def push(self, x):        """       

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

[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