【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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, and is 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)
(二)解题
题目大意:用queue来实现一个stack。
解题思路:queue是先进先出,stack是先进后出。采用一个queue就能实现stack
push:直接压入到queue
pop:需要弹出queue的尾元素,可是queue只能弹出queue的头元素。这时候可以把queue的头元素弹出再压入queue,一直到尾元素弹出即可
top:直接利用queue的back()即可知道stack顶元素
empty:判断queue是否为空。
class Stack {
public:
// Push element x onto stack.
void push(int x) {
que.push(x);
}
// Removes the element on top of the stack.
void pop() {
int size = que.size();
for(int i = 0 ; i < size-1 ;i++){//queue的头元素弹出再压入,直到尾元素弹出
que.push(que.front());
que.pop();
}
que.pop();
}
// Get the top element.
int top() {
return que.back();
}
// Return whether the stack is empty.
bool empty() {
if(que.empty()) return true;
return false;
}
private:
queue<int> que;
};
【一天一道LeetCode】#225. Implement Stack using Queues的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- 【Uva 10269 马里奥与公主的归途】
·马里奥n次解救了公主,现在需要从魔王的宫殿返回. ·英文题,述大意: 给定一个点数不超过100的无向图,其中的点分为两类:乡村和城堡. 输入A个乡村,B个城堡(乡村编号1~A,城堡编号A+ ...
- Linux查看CPU、内存、进程使用情况(转)
在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...
- SSH构造struts2项目
第一在pom.xml导入相应的包 (网上有很多导入多个包的教程,我缩减到一个了) <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- Mobx使用详解
Mobx是一个功能强大,上手非常容易的状态管理工具.就连redux的作者也曾经向大家推荐过它,在不少情况下你的确可以使用Mobx来替代掉redux. 本教程旨在介绍其用法及概念,并重点介绍其与Reac ...
- mongo数据删除和游标
数据删除 db.集合.remove(删除条件,是否只删除一个数据);默认删多条(false)true删除一条db.集合.remove({}) 删除所有元素但集合还在db.集合.drop() 删除集合 ...
- lodop打印收费小票
//收费系统打印机功能:收费/退费,需要使用到lodop var LODOP;//打印机 $(function () { //初始化 $("body").append('<o ...
- OpenCV3.1.0中调用MHI(Motion History Images, 运动历史图像)
写在前边: OpenCV3.0+要想使用MHI,就要现安装扩展模块opencv_contrib.安装方法见:ubuntu 14.04 64位 安装Opencv3.1.0 (包含opencv_contr ...
- MongoDB 查询文档
语法 MongoDB 查询数据的语法格式如下: >db.COLLECTION_NAME.find() find() 方法以非结构化的方式来显示所有文档. 如果你需要以易读的方式来读取数据,可以使 ...
- C/C++静态数组与动态数组的区别
简介 以下三行代码有什么区别? int a[10]; int *a = (int*)malloc(sizeof(int)*10); int *a = new int[10]; 第一行代码定义a为包含1 ...
- Activtiy完全解析(三、View的显示过程measure、layout、draw)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客] 在Activity完全解析的第一篇文章A ...