【LeetCode232】 Implement Queue using Stacks★
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★的更多相关文章
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks
class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...
- 【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( ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
随机推荐
- 【代码笔记】Web-利用Dreamweaver实现表格
一,打开Dreamweaver---->File---New---->如下图所示.选择HTML,点击OK. 二,会出现如下图所示界面.把光标放到Body处. 三,Insert---> ...
- 解决如下问题:You are using pip version 8.1.1, however version 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
问题描述: 今天想学习一下TUM数据集RGBD-Benchmark工具的使用,利用python进行相关操作时,缺少一个第三方模块,于是打算用pip进行安装,便出现如下图所示的问题. 解决办法: 执行如 ...
- How to deploy kotlin in androidstudio?
Kotlin is a good language,more playable than Java.So how do we deploy kotlin? First,install the kotl ...
- 你不可不知的Java引用类型之——软引用
定义 软引用是使用SoftReference创建的引用,强度弱于强引用,被其引用的对象在内存不足的时候会被回收,不会产生内存溢出. 说明 软引用,顾名思义就是比较"软"一点的引用. ...
- VMware-workstation12.5.6 新建虚拟机 安装 centos6.5
1.到官方下载镜像文件 注意:看到上面那两个红框不要着急复制链接用迅雷下载,因为点击进去是个页面,不知道官方做这个用意是怎样的,个人感觉很傻缺我们只要点击第一个红框就行 *************** ...
- Spring Boot 中配置文件application.properties使用
一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...
- 洗礼灵魂,修炼python(51)--爬虫篇—变色龙般的伪装
变色龙原理 变色龙这种动物想必大家都了解,它们会根据周遭环境的局势来改变自己的颜色,伪装自己. 那么爬虫有这种技能吗?当然是有的,先不着急说这个问题. 从上一篇开始,你有没有想过,站在网站管理的角度, ...
- Linux 小知识翻译 - 「Linux」和病毒
据说,「Linux」系统上的病毒要远远少于Windows系统上病毒.从2种系统的普及度来看,这是很显然的, 「Linux」的使用人群很少,所以「Linux」上的病毒的扩散时,受害的范围也不大. 但是, ...
- 简易 Token 验证的实现
简易 Token 验证的实现 前言 在我们的服务器和客户端的交互中,由于我们的业务中使用 RESTful API 的形式和客户端交互,而 API 又是无状态的,无法帮助我们识别这一次和上一次的请求由谁 ...
- MySQL视图 索引 存储过程 触发器 函数
视图: 也就是一个虚拟表(不是真实存在的),它的本质就是根据SQL语句获取动态的数据集,并为其命名.用户使用时只需要使用命名的视图即可获取结果集,并可以当做表来使用.它的作用就是方便查询操作,减 ...