【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() -- 从 ...
随机推荐
- JS中数组去重的九方法
数组去重方法 方法一:运用set结构特点:存储的数据没有重复的,结果为对象,再用Array.from()转换成数组 var arr = [1,1,2,1,3,4,5] ...
- Git应用--04遇到冲突解决办法git stash(转载)
git merge git pull时候遇到冲突解决办法git stash https://www.cnblogs.com/juandx/p/5362723.html 在使用git pull代码时,经 ...
- (网页)JQuery 对 Select option 的操作(转)
转自博客园: <select id="selectID" > <option value="1">1</option> &l ...
- (网页)web性能优化(转)
转自CSDN: Web性能优化分为服务器端和浏览器端两个方面. 一.浏览器端,关于浏览器端优化,分很多个方面1.压缩源码和图片JavaScript文件源代码可以采用混淆压缩的方式,CSS文件源代码进行 ...
- 常用的Git命令整理
之前一直忙于项目苦于没有时间总结,今天刚好有时间特来总结一下在工作中常用到的代码版本管理器Git.至于为什么要用Git?Git相比SVN有哪些好处?我就不多说了,前人已经总结的很好.今天主要介绍的是常 ...
- [20170914]tnsnames.ora的管理.txt
[20170914]tnsnames.ora的管理.txt --//昨天朋友讲tnsnams.ora的内容太长了,而且许多不需要的.管理不方便.我记得以前写[20150409]tnsnames.ora ...
- 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment
闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...
- Linux运维之每日小技巧-检测网站状态以及PV、UV等介绍
[root@ELK-chaofeng07 httpd]# curl -o /dev/null -w %{http_code}\\n -s www.baidu.com 状态码为200表示成功. PV.U ...
- Alpha冲刺! Day12 - 砍柴
Alpha冲刺! Day12 - 砍柴 今日已完成 晨瑶:终于更了 Gitkraken 团队协作教程. 昭锡:初步学习了解Android动画. 永盛:用户逻辑基本完成. 立强:从众多开源库中找到两个合 ...
- 简单Nginx下防跨站、跨目录安全设置,支持PHP 5.3.3以上版本
Nginx下存在跨站和跨目录的问题,跨站和跨目录影响同服务器/VPS上的其他网站. PHP在5.3.3以上已经增加了HOST配置,可以起到防跨站.跨目录的问题. 如果你是PHP 5.3.3以上的版本, ...