1.题目描述

2.思路

  思路简单,这里用一个图来举例说明:

3.java代码

 public class MyQueue {

    Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>(); /** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
if(!stack2.isEmpty())
return stack2.pop();
else{
while(!stack1.empty())
stack2.push(stack1.pop());
return stack2.pop();
}
} /** Get the front element. */
public int peek() {
if(!stack2.isEmpty())
return stack2.peek();
else{
while(!stack1.empty())
stack2.push(stack1.pop());
return stack2.peek();
}
} /** Returns whether the queue is empty. */
public boolean empty() {
return stack1.empty()&&stack2.empty();
}
} /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/

【LeetCode232】 Implement Queue using Stacks★的更多相关文章

  1. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  2. 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks

    class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...

  3. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  4. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  5. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  6. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  7. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  8. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  9. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

随机推荐

  1. Oracle绑定变量在C#.NET中的应用及意义

    一. 什么是绑定变量 绑定变量(bind variable) : select * from emp where empno=:empno; 是用户放入查询中的占位符,它会告诉Oracle“我会随后为 ...

  2. Android View体系(七)从源码解析View的measure流程

    前言 在上一篇我们了解了Activity的构成后,开始了解一下View的工作流程,就是measure.layout和draw.measure用来测量View的宽高,layout用来确定View的位置, ...

  3. CSS3伪类和伪元素

    作为一个CSS3初学不久者来说,很容易混淆单冒号(:)和双冒号(::)的用法,以为两者可以互换着来使用.我自己之前也混淆过他们,因为两者看起来太相像了,就像孪生兄弟.但实际上,他们的区别还是挺大的,最 ...

  4. mysqlclient and mysql-python安装出错方法

    Collecting mysql-python Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68d ...

  5. weblogic系列漏洞整理 -- 2. weblogic弱口令

    目录 二. weblogic弱口令 0. 思路 1. python爆破脚本 2. 技巧 一.weblogic安装 http://www.cnblogs.com/0x4D75/p/8916428.htm ...

  6. [20170825]不启动监听远程能连接数据库吗2.txt

    [20170825]不启动监听远程能连接数据库吗2.txt --//曾经写过一篇不启动监听连接数据库的帖子:http://blog.itpub.net/267265/viewspace-1816211 ...

  7. [cb]ScriptableWizard 创建向导

    需求 方便策划一步一步的创建Actor 思路分析 Unity的Editor中提供创建向导的功能,ScriptableWizard 代码实现 创建一个WizardCreateActor继承自Script ...

  8. Ubuntu关闭(重启)网络服务命令

    Ubuntu关闭网络服务命令: service network-manager stop 重启网络服务命令: service network-manager restart

  9. VScode启动后cup100%占用的解决方法

    新安装的vscode,版本1.29.1.启动后,cpu占用一直是100%,非常的卡.百度以下,找到了解决方法,整理一下. 解决方法:在VScode中文件->首选项->设置->搜索-& ...

  10. switch选择结构

    switch( 表达式 )//表达式可以使用byte,short,int,char { case 值1: 逻辑语句: break;//跳出switch语句 case 值2: 逻辑语句; break; ...