leetcode oj java Implement Stack using Queues

原创 2015年11月17日 21:26:51

问题描述:

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 standard operations of a queue -- which means onlypush to back, peek/pop from front, size, andis 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).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.


思路: 用两个队列实现一个栈 q1 作为专用的队列,q2作为中转站

入栈: 队列的offer() 或者add();

出栈:将q1的前q1.size()-1个依次放入q2中,poll()出q1中剩下的一个,然后再将q2的依次放入q1中。

top   : 如果队列中只有一个元素,取出即可; 如果队列中有多个元素,先将q1.size()-1个依次放入q2中,取出q1中剩下的,作为top值返回,再依次还原。

empty:   队列的isEmpty() 方法,


package leetcode;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    Queue<Integer> q1 = new LinkedList<Integer>();
    Queue<Integer> q2 = new LinkedList<Integer>();
    // Push element x onto stack.
    public void push(int x) {
        q1.offer(x);
    }

    // Removes the element on top of the stack.
    public void pop() {
        int a = q1.size();
        if(a==1){ q1.poll();}
        else{
        for(int i = 0; i<a-1; i++){
            q2.add(q1.peek());
            q1.poll();
        }
        q1.poll();
        int b = a;
        for(int i = 0; i<b-1; i++){
            q1.add(q2.peek());
            q2.poll();
        }}
    }

    // Get the top element.
    public int top() {
        int a = 0;
        if(q1.size()==1){a = q1.peek();}
        else{
        int m = q1.size();
        for(int i = 0; i<m-1; i++){
            q2.add(q1.peek());
            q1.poll();
        }
        a = q1.peek();      
        q2.add(q1.peek());
        q1.poll();
        int n = q2.size();
        for(int i = 0; i<n; i++){
            q1.add(q2.peek());
            q2.poll();
        }}
//        int a = q1.peek();
        return a;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q1.isEmpty();
    }
}


提交的状态:

16 / 16 test cases passed.
Status:

Accepted

Runtime: 110 ms
Submitted: 28 minutes ago



/----------------------------我是华丽丽的分割线--------------------------/

自己编写了主函数测试:

package leetcode;

public class Stack {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyStack a = new MyStack();
        a.push(1);
        a.push(2);
        a.push(3);
        System.out.println(a.top());
        a.pop();
        System.out.println(a.top());

        
    }

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目:Implement the following operati...

<LeetCode OJ> 225. Implement Stack using Queues

225. Implement Stack using Queues My Submissions Question Total Accepted: 26676 Total Submission...

【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】

【225-Implement Stack using Queues(用队列实现栈操作)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.co...

leetcode 225. Implement Stack using Queues

题目Implement the following operations of a stack using queues.push(x) – Push element x onto stack. p...

关于LeetCode中Implement Stack using Queues一题的理解

题目如下: Implement the following operations of a stack using queues. push(x) -- Push element x on...

[leetcode: Python]225. Implement Stack using Queues

题目: mplement the following operations of a stack using queues.push(x) – Push element x onto stack. ...
  • NXHYD
  • NXHYD
  • 2017-05-12 12:54
  • 161

Leetcode225. Implement Stack using Queues

该题和232题类似,思想上差不多232题 原题 Implement the following operations of a stack using queues.push(x) – Push ...

LeetCode(36)- Implement Stack using Queues

题目:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack. ...

【LeetCode】Implement Stack using Queues 解题报告

【题目】 Implement the following operations of a stack using queues. push(x) -- Push element x o...

【leetcode】【225】Implement Stack using Queues

一、问题描述 Implement the following operations of a stack using queues. push(x) -- Push element x...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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