【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/
题目:Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Notes:
- You must use only standard operations of a stack -- which means only
push
,
to toppeek/pop from top
,size
,
andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
/**
* 用两个栈模拟实现队列基本操作
* @author 徐剑
* @date 2016-02-25
*
*/
public class Solution
{
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
// Push element x to the back of queue.
public void push(int x)
{
s1.push(x); }
// Removes the element from in front of queue.
public void pop()
{
if (!s2.empty())
{
s2.pop();
} else if (s1.isEmpty())
{
return;
} else
{
while (!s1.isEmpty())
{
int temp = s1.pop();
s2.push(temp);
}
s2.pop();
}
}
// Get the front element.
public int peek()
{
if (!s2.isEmpty())
return s2.pop(); else if (s1.isEmpty())
{
return -1;
} else
{
while (!s1.isEmpty())
{
int temp = s1.pop();
s2.push(temp);
}
return s2.peek();
}
}
// Return whether the queue is empty.
public boolean empty()
{
return s1.isEmpty() && s2.isEmpty();
}
}
【LeetCode OJ 232】Implement Queue using Stacks的更多相关文章
- LeetCode(232) Implement Queue using Stacks
题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- 【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 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...
- LeetCode 232: Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 【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 OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- 【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...
- 【LeetCode232】 Implement Queue using Stacks★
1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
随机推荐
- Codeforces Round #419
A Karen and Morning 找最近的回文时间 模拟 往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...
- 2CSS层叠规则(即引入CSS的三种不同方式的优先级)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Java系列学习(二)-配置开发环境
1.设置系统环境变量 1.1.设置JDK的Path路径 作用:通过path环境变量,将JDK安装目录下的bin目录配置到path变量下,可使javac指令和java指令在任意目录下运行 方法一:直 ...
- Java系列学习(零)-写在前面的话
1.为什么写这套笔记 理由一:因为需求,所以学习,然后就要记笔记 理由二:同时学几种相似的语言,怕搞混,所以写 2.笔记修改日志
- [Android]有关外部链接唤醒App需要注意的坑
移动互联网发展到今天,一个移动app需要和各种各样的外部链接关联,它不再仅仅从手机的桌面启动,更多的将会从其他的应用.浏览器链接.短信.二维码或者微信分享等渠道启动,这里涉及到的是各种各样的营销渠道和 ...
- Ajax——模板引擎
模板介绍 1.必要性:ajax请求从服务器接收到大量数据,此时再用普通的字符串拼接是很耗费时间的,这时候模板就有其必要性 2.便利性:插件套用,现在有很多主流的模板插件:BaiduTemplate(百 ...
- python函数参数的区别
在运用python的过程中,发现当函数参数为list的时候,在函数内部调用list.append()会改变形参,与C/C++的不太一样,查阅相关资料,在这里记录一下. python中id可以获取对象的 ...
- day13-迭代器、三元表达式、列表推导式、字典生成式、生成器与递归
目录 迭代器 可迭代对象 迭代器对象 for循环原理 三元表达式(三目表达式) 列表推导式 字典生成式 zip()方法 生成器 生成器表达式 递归 递归的两个阶段 迭代器 迭代器即迭代的工具,迭代是一 ...
- CAD设置当前显示的光标(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SetCursor 设置当前显示的光标,光标可以从cur文件加载,详细说明如下: 参数 说明 CString sCursorFi ...
- linu下nginx的安装
这里用到的环境是nginx-1.8.0,linux用的是CentOS-7-x86_64-DVD-1804.iso版本 1 什么是nginx Nginx ("engine x") ...