【LeetCode 225_数据结构_栈】Implement Stack using Queues

 1 class Stack {
 2 public:
 3     // Push element x onto stack.
 4     void push(int x) {
 5         int len = nums.size();
 6         nums.push(x);
 7         for (int i = 0; i < len; ++i) {
 8             nums.push(nums.front());
 9             nums.pop();
10         }
11     }
12
13     // Removes the element on top of the stack.
14     void pop() {
15         nums.pop();
16     }
17
18     // Get the top element.
19     int top() {
20         return nums.front();
21     }
22
23     // Return whether the stack is empty.
24     bool empty() {
25         return nums.empty();
26     }
27 private:
28     queue<int> nums;
29 };
时间: 2024-10-12 14:44:24

【LeetCode 225_数据结构_栈】Implement Stack using Queues的相关文章

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

Leetcode题解——数据结构之栈和队列

1. 用栈实现队列 2. 用队列实现栈 3. 最小值栈 4. 用栈实现括号匹配 5. 数组中元素与下一个比它大的元素之间的距离 6. 循环数组中比当前元素大的下一个元素 1. 用栈实现队列 232. Implement Queue using Stacks (Easy) 栈的顺序为后进先出,而队列的顺序为先进先出.使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序. class MyQueue { private

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 利用队列构建栈 ---------- 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 OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/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. empt

Implement Stack using Queues leetcode

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. 不用多想,可以使用两个queue来实现,代码如下 耗

leetcode 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