Java语言实现版,在push时候做翻转
class MyStack {
Queue<Integer> queue=new LinkedList<Integer>();
/** Push element x onto stack. */
public void push(int x) {
Queue<Integer> temp=new LinkedList<Integer>();
while (!queue.isEmpty()){
temp.add(queue.poll());
}
queue.add(x);
while (!temp.isEmpty()){
queue.add(temp.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
}
/** Get the top element. */
public int top() {
return queue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
作者回复: 赞!
2019-04-01
2
Derek
GO语言版实现:
type MyQueue struct {
inStack []int
outStack []int
}
/** Initialize your data structure here. */
func Constructor() MyQueue {
return MyQueue{
inStack: make([]int, 0),
outStack: make([]int, 0),
}
}
/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
this.inStack = append(this.inStack, x)
}
/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
for _, v := range this.inStack {
this.outStack = append(this.outStack, v)
}
this.inStack = make([]int, 0)
v := this.outStack[0]
this.outStack = this.outStack[1 : len(this.outStack)-1]
return v
}
/** Get the front element. */
func (this *MyQueue) Peek() int {
for _, v := range this.inStack {
this.outStack = append(this.outStack, v)
}
this.inStack = make([]int, 0)
return this.outStack[0]
}
/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
return len(this.inStack) != 0 || len(this.outStack) != 0
}
/**
* Your MyQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/
老师,我刚刚又优化了下速度,因为刚刚在leetcode试了之前的方法才击败了50%,现在这个击败96%
# -*- coding: UTF-8 -*
class MyQueue(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.input_stack = []
self.output_stack = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: None
"""
self.input_stack.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if self.empty():
return None
else:
if len(self.output_stack):
return self.output_stack.pop()
else:
# for i in xrange(0, len(self.input_stack)):
# self.output_stack.append(self.input_stack.pop())
length = len(self.input_stack)
self.output_stack = map(lambda x: self.input_stack.pop(), range(0, length))
return self.output_stack.pop()
def peek(self):
"""
Get the front element.
:rtype: int
"""
if self.empty():
return None
else:
if len(self.output_stack):
return self.output_stack[-1]
else:
# for i in xrange(0, len(self.input_stack)):
# self.output_stack.append(self.input_stack.pop())
length = len(self.input_stack)
self.output_stack = map(lambda x: self.input_stack.pop(), range(0, length))
return self.output_stack[-1]
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return bool(len(self.input_stack) == 0 and len(self.output_stack) == 0)
2019-07-16
3
编码者
public class MyQueue
{
Stack<int> _inputStack;
Stack<int> _outputStack;
/** Initialize your data structure here. */
public MyQueue()
{
_inputStack = new Stack<int>();
_outputStack = new Stack<int>();
}
/** Push element x to the back of queue. */
public void Push(int x)
{
_inputStack.Push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int Pop()
{
MoveInputToOutput();
return _outputStack.Pop();
}
/** Get the front element. */
public int Peek()
{
MoveInputToOutput();
return _outputStack.Peek();
}
/** Returns whether the queue is empty. */
public bool Empty()
{
return _inputStack.Count == 0 && _outputStack.Count == 0;
}
private void MoveInputToOutput()
{
if (_outputStack.Count == 0)
{
while (_inputStack.Count != 0)
{
_outputStack.Push(_inputStack.Pop());
}
}
}
}
2020-04-16
2
pikachu122
LeetCode232 Java代码 0 ms 34.2 MB 打败100%Java提交
class MyQueue {
Stack<Integer> in = new Stack<>();
Stack<Integer> out = new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
in.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
return out.pop();
}
/** Get the front element. */
public int peek() {
if (out.isEmpty()){
while (!in.isEmpty()) {
out.push(in.pop());
}
}
return out.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return in.isEmpty() && out.isEmpty();
}
}