LeetCode——Implement Stack using Queues
Description:
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
用队列来实现一个栈。
思路:用两个队列来模拟栈的功能,当前队列(cur)是用来存放数据的,另一个队列是用来替换当前队列的,这样就能操作队尾元素了。两个队列来回切换,一个用来保存数据,另一个用来操作队尾元素。
PS.Java里不能用泛型数组啊,只好用线性表来代替了。代码看起来又复杂了一些。要不要用C++和Python再来一遍。
- class MyStack {
- public List<Queue<Integer>> queue;
- public int cur;
- public MyStack() {
- queue = new ArrayList<Queue<Integer>>();
- queue.add(new LinkedList<Integer>());
- queue.add(new LinkedList<Integer>());
- cur = 0;
- }
- // Push element x onto stack.
- public void push(int x) {
- queue.get(cur).offer(x);
- }
- // Removes the element on top of the stack.
- public void pop() {
- while(queue.get(cur).size() > 1) {
- queue.get(1-cur).offer(queue.get(cur).poll());
- }
- queue.get(cur).poll();
- cur = 1 - cur;
- }
- // Get the top element.
- public int top() {
- while(queue.get(cur).size() > 1) {
- queue.get(1-cur).offer(queue.get(cur).poll());
- }
- int t = queue.get(cur).poll();
- queue.get(1-cur).offer(t);
- cur = 1 - cur;
- return t;
- }
- // Return whether the stack is empty.
- public boolean empty() {
- if(queue.get(cur).isEmpty())
- return true;
- else
- return false;
- }
- }
LeetCode——Implement Stack using Queues的更多相关文章
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode Implement Stack using Queues (数据结构)
题意: 用队列来实现栈. 思路: 没有什么捷径,纯粹模拟.但是用一个队列就够了. class Stack { /* // Push element x onto stack. void push(in ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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 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 ...
- lc面试准备:Implement Stack using Queues
1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
随机推荐
- Hadoop Balancer源代码解读
前言 近期在做一些Hadoop运维的相关工作,发现了一个有趣的问题,我们公司的Hadoop集群磁盘占比数值參差不齐,高的接近80%.低的接近40%.并没有充分利用好上面的资源,可是balance的操作 ...
- HISTTIMEFORMAT 设置历史命令时间的格式
echo 'HISTTIMEFORMAT="%F %T `whoami`" ' >>/etc/bashrc whoami 完了后面要有空格不然会连住和命令 ===== ...
- .net垃圾回收
垃圾回收器(gc)用来在.NET中进行内存管理,特别是它可以恢复正在运行的应用程序需要的内存. .NET运行库采用的方法是垃圾回收器,这是一个程序,其目的是清理内存.方法是所有动态请求的内存都分配到堆 ...
- Mac之安装zsh
1.安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...
- 转载:【原译】Erlang构建和匹配二进制数据(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/19/2727204.html Constructing and matching binarie ...
- CentOS系统资源常用命令
系统: # uname -a # 查看内核/操作系统/CPU信息 # cat /etc/issue # cat /etc/redhat-release # 查看操作系统版本 # cat /proc ...
- 在VS中写js的同学注意了。。。。。。。。。。。。。。。。。。。
在vs中安装扩展jsdoc就可以实现这个功能
- Centos6.8/7.0搭建Git服务http访问方式
安装Git版本:git 2.10.0 Git访问方式:基于http的基本验证(非SSL) 1. 安装Apache软件: [root@localhost ~]# yum install httpd 设置 ...
- Hibernate学习(1):查询demo
1.数据库(mysql)创建脚本 DROP TABLE IF EXISTS role; CREATE TABLE IF NOT EXISTS `role`( `id` ) NOT NULL AUTO_ ...
- informatica中的workflow连接远程数据库
如果是远程oracle这样写 名称随便起,方便自己记住,后面用户名密码你都知道,再加上数据库的地址:端口/SID就可以了. 如10.33.2.208:1521/torcl