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. SEO高手和SEO屌丝的八个区

    原文:http://www.it28.cn/sousuoyinqing/853085.html SEO这个行业并不规范,有些seo工程师可以拿着高薪,进行一些大型网站的seo工作,其实主要是UEO的工 ...

  2. Java并发编程(十一)线程池的使用

    1.new Thread的弊端如下: a. 每次new Thread新建对象性能差. b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom. c. 缺乏更多 ...

  3. gson解析json

    json介绍 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript 规范的一个子集,采用完全独立于编程语言的文本 ...

  4. Fragment 中 ListView绑定ContextMenu

    package com.example.administrator.imbobo.controller.fragment; import android.content.BroadcastReceiv ...

  5. AOP缓存实现

    输入参数索引作为缓存键的实现 using MJD.Framework.CrossCutting; using MJD.Framework.ICache; using System; using Sys ...

  6. 4.92Python数据类型之(7)字典

    目录 目录 前言 (一)字典的基本知识 ==1.字典的基本格式== (二)字典的操作 ==1.字典元素的增加== ==2.字典值的查找== ==3.字典的修改== ==4.字典的删除== ==5.字典 ...

  7. 阿里八八β阶段Scrum(5/5)

    今日进度 陈裕鹏: 简单信息抽取编码完成 叶文滔: 处理了信息抽取编码的一些BUG,修复了日程界面不会自动更新添加的日程的BUG,修改了原先测试用的TAG以及数据分析部分数据计算数值错误的问题 王国超 ...

  8. 回文数的golang实现

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数 输入: 输出: true 输入: - 输出: false 解释: 从左向右读, 为 - . 从右向左读, 为 - ...

  9. Nginx禁止目录执行php文件权限

    location ~ /dir/.*.(php|php5)?$ { deny all; } 禁止dir目录执行php文件权限 .csharpcode, .csharpcode pre { font-s ...

  10. 轻量级服务器部署方案 -(阿里云CenOS+宝塔)

    一台服务器部署多个应用.可部署前端工程.node服务.数据库等. 一.服务器选择合适的服务器,购买即可.服务器商1.阿里云 2.腾讯云 3.百度云 二.服务器管理面板-宝塔宝塔面板是一款服务器管理软件 ...