【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() -- 从 ...
随机推荐
- Ajax的实现及使用-原生对象
正文 学习JavaScript就不得不提到Ajax,从2005年开始,Ajax技术就开始席卷整个Web世界.作为一个前端来说,大部分时间中都是使用的库中封装好的ajax模块(jQuery),即使已使用 ...
- 我的Java之旅 第六课 JAVA WEB 请求与响应
一.有关URL编码 1.在URL的规范中定义了一些保留字符,如:: / ? & = @ % 等,在URI中有它的作用.如果要在URI中包含这些字符,必须转码,即%字符后跟十六进 ...
- Android--px(像素)和dp、sp之间的相互转化
public class DensityUtil { public DensityUtil() { } public static int dip2px(Context var0, float var ...
- springcloud 入门 8 (config配置中心)
Spring Cloud Config: 配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config. 在分布式系统中,由于服务数量巨多, ...
- python内置小工具
python -m http.server # 启动一个下载服务器 echo '{"job": "developer", "job": &q ...
- Iptables防火墙(SNAT和DNAT)
1.SNAT:源地址转换 实现内网访问外网,修改IP地址,使用POSTROUTING 命令:iptables -t nat -A POSTROUTING -s 192.168.1.10/2 ...
- django中admin
我们在models中建立了表结构,想要在admin中表示: from django.contrib import admin from . import models for table in mod ...
- log4.net 配置 - StringMatchFilter过滤器的使用
当我们需要对log4输出的内容进行过滤时就需要使用到StringMatchFilter过滤器 它有两种工作模式: 1.字符串查找模式:只要消息内容包含指定字符串则符合过滤器规则. 2.正则表达式模式: ...
- PJ初赛复习日记
PA姑娘的PJ初赛复习日记 by Pleiades_Antares PJ初赛考试马上就要开始了(今年应该是10.13吧?),作为蒟蒻的我们怎么能不复习呢? 众所周知,复习方法有很多很多种-- 比如 ( ...
- Beta冲刺博客集合贴
Beta阶段第一次冲刺 Beta阶段第二次冲刺 Beta阶段第三次冲刺 Beta阶段第四次冲刺 Beta阶段第五次冲刺 Beta阶段总结博客