Leetcode 225,232 用两个队列实现栈,用两个栈实现队列

Leetcode 225,232 用两个队列实现栈,用两个栈实现队列

232 Implement Queue using Stacks My Submissions Question
Total Accepted: 29497 Total Submissions: 86968 Difficulty: Easy
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 must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).**

s1,s2两个栈,出队列时如果s2为空,将s1的元素全部Pop到s2再从s2 Pop。否则直接s2 Pop。
入队列时直接s1 Push

代码


public class Queue {
    Stack<int> s1 = new Stack<int>();
    Stack<int> s2 = new Stack<int>();
    // Push element x to the back of queue.
    public void Push(int x) {
        s1.Push(x);
    }

    // Removes the element from front of queue.
    public void Pop() {
        if(s2.Count==0) {
           while(s1.Count>0) s2.Push(s1.Pop());
        }
        s2.Pop();
    }

    // Get the front element.
    public int Peek() {
        if(s2.Count==0) {
           while(s1.Count>0) s2.Push(s1.Pop());
        }
        return s2.Peek();
    }

    // Return whether the queue is empty.
    public bool Empty() {
        return s1.Count==0 && s2.Count==0;
    }
}

**225 Implement Stack using Queues My Submissions Question
Total Accepted: 27669 Total Submissions: 91210 Difficulty: Easy
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 back, peek/pop from front, size, 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).**

感觉这个方法比较笨,不知道有没有更好的。始终保持一个队列为空。入队列入非空的队列。出队列时把非空队列的除最后一个外全部转移到空的队列,然后出非空队列最后一个。

代码

public class Stack {
    Queue q1=new Queue(),q2=new Queue();
    // Push element x onto stack.
    public void Push(int x) {
        if(q1.Count!=0) q1.Enqueue(x);
        else if(q2.Count!=0) q2.Enqueue(x);
        else q1.Enqueue(x);
    }

    // Removes the element on top of the stack.
    public void Pop() {
        Queue q = q1.Count==0?q2:q1;
        Queue qq = q1.Count==0?q1:q2;
        while(q.Count>1) {
            qq.Enqueue(q.Dequeue());
        }
        q.Dequeue();
    }

    // Get the top element.
    public int Top() {
        Queue q = q1.Count==0?q2:q1;
        Queue qq = q1.Count==0?q1:q2;
        while(q.Count>1) {
            qq.Enqueue(q.Dequeue());
        }
        int ret = (int)q.Peek();
        qq.Enqueue(q.Dequeue());
        return ret;
    }

    // Return whether the stack is empty.
    public bool Empty() {
        return q1.Count==0 && q2.Count==0;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值