leetcode 232:Implement Queue using Stacks

原创 2015年07月07日 04:48:38

Implement Queue using Stacks

Total Accepted: 93 Total Submissions: 256

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 onlypush 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).


[思路]

用两个stacks实现一个queue.

[CODE]

class MyQueue {
    Stack<Integer> s1 = new Stack<>();
    Stack<Integer> s2 = new Stack<>();

    // Push element x to the back of queue.
    public void push(int x) {
        s1.push(x);
    }

    // Removes the element from in front of queue.
    public void pop() {
        if(!s2.isEmpty()) s2.pop();
        else {
            while(!s1.isEmpty()) s2.push(s1.pop());
            s2.pop();
        }
    }

    // Get the front element.
    public int peek() {
        if(!s2.isEmpty()) return s2.peek();
        else {
            while(!s1.isEmpty()) s2.push(s1.pop());
            return s2.peek();
        }
    }

    // Return whether the queue is empty.
    public boolean empty() {
        return s1.empty() && s2.empty();
    }
}



【LeetCode-232】 Implement Queue using Stacks(C++)

题目要求:用站实现队列的插入,删除,返回队头元素和判断是否为空四种操作。 解题方法:用两个栈实现队列。代码如下: class Queue { public: // Push element...
  • liujiayu1015
  • liujiayu1015
  • 2016年09月03日 17:37
  • 243

LeetCode 232: Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back...
  • sunao2002002
  • sunao2002002
  • 2015年07月07日 19:35
  • 4281

leetcode-232-Implement Queue using Stacks

问题题目:[leetcode-232]思路借助一个辅助栈去实现。把一个栈导入另一个栈,辅助的操作可以写成一个函数。代码class MyQueue { public: /** Initializ...
  • Kang_TJU
  • Kang_TJU
  • 2017年02月19日 10:56
  • 159

LeetCode232—Implement Queue using Stacks

LeetCode232: Implement Queue using Stacks
  • zhangxiao93
  • zhangxiao93
  • 2016年10月30日 21:32
  • 314

LeetCode232:Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...
  • u012501459
  • u012501459
  • 2015年07月10日 23:08
  • 1007

Leetcode_232_Implement Queue using Stacks

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392363 Implement the foll...
  • pistolove
  • pistolove
  • 2015年09月12日 11:01
  • 1338

leetcode 232. Implement Queue using Stacks

题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to th...
  • Chendehaial
  • Chendehaial
  • 2016年07月05日 22:12
  • 142

<LeetCode OJ> 232. Implement Queue using Stacks

232. Implement Queue using Stacks My Submissions Question Total Accepted: 28236 Total Submission...
  • EbowTang
  • EbowTang
  • 2015年12月31日 21:30
  • 748

【LeetCode OJ 232】Implement Queue using Stacks

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operati...
  • xujian_2014
  • xujian_2014
  • 2016年02月25日 21:02
  • 901

LeetCode232——Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back...
  • booirror
  • booirror
  • 2015年07月31日 16:48
  • 580
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 232:Implement Queue using Stacks
举报原因:
原因补充:

(最多只允许输入30个字)