LeetCode-232 Implement Queue using Stacks Solution (with Java)

1. Description:

Notes: 

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-03-07
 3  * Your MyQueue object will be instantiated and called as such:
 4  * MyQueue obj = new MyQueue();
 5  * obj.push(x);
 6  * int param_2 = obj.pop();
 7  * int param_3 = obj.peek();
 8  * boolean param_4 = obj.empty();
 9  */
10 class MyQueue {
11     /** Initialize your data structure here. */
12     Stack<Integer> input = new Stack<Integer>();
13     Stack<Integer> output = new Stack<Integer>();
14
15     /** Push element x to the back of queue. */
16     public void push(int x) {
17         input.push(x);
18     }
19
20     /** Removes the element from in front of queue and returns that element. */
21     public int pop() {
22         peek();
23         return output.pop();
24     }
25
26     /** Get the front element. */
27     public int peek() {
28        if(output.empty()){
29            while (!input.empty()){
30                output.push(input.pop());
31            }
32        }
33        return output.peek();
34     }
35
36     /** Returns whether the queue is empty. */
37     public boolean empty() {
38         return input.empty() && output.empty();
39     }
40 }

原文地址:https://www.cnblogs.com/sheepcore/p/12395208.html

时间: 2024-07-31 20:26:20

LeetCode-232 Implement Queue using Stacks Solution (with Java)的相关文章

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

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

[LeetCode] 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 whether the queue is empty. Notes: You

[LeetCode] 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 whether the queue is empty. Notes: You

[LeetCode] 232. Implement Queue using Stacks Java

题目: 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 whether the queue is empty. Notes:

(easy)LeetCode 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 whether the queue is empty. Notes: You

Java [Leetcode 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 whether the queue is empty. Note

Java for LeetCode 232 Implement Queue using Stacks

Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // Removes the element from in front of queue. public void pop() { stack.remove(0); } // Get the front element. public int peek() { return stack.get(0); } //

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