LeetCode Implement Stack using Queues (数据结构)
题意:
用队列来实现栈。
思路:
没有什么捷径,纯粹模拟。但是用一个队列就够了。
class Stack {
/*
// Push element x onto stack.
void push(int x) {
}
// Removes the element on top of the stack.
void pop() {
}
// Get the top element.
int top() {
}
// Return whether the stack is empty.
bool empty() {
}
*/
queue<int> que;
public:
void push(int x)
{
que.push(x);
}
void pop(void)
{
if(!que.empty())
{
int n=que.size(),pos=;
while(pos<n)
{
que.push(que.front());
que.pop();
pos++;
}
que.pop();
}
}
int top()
{
if(!que.empty())
{
int n=que.size(), pos=,x=;
while(pos<=n)
{
if(pos==n) x=que.front();
que.push(que.front());
que.pop();
pos++;
}
return x;
}
}
bool empty(void)
{
if(que.empty()) return true;
else return false;
}
};
AC代码
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
Description: Implement the following operations of a stack using queues. push(x) -- Push element x o ...
- 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) -- ...
随机推荐
- 【转】数据库范式(1NF 2NF 3NF BCNF)详解二
以下内容转自:http://jacki6.iteye.com/blog/774889 -------------------------分割线----------------------------- ...
- dll和ocx比较
ActiveX,OLE是基于COM的一种应用,其文件后缀一般以dll和ocx结尾:ocx作为一种特殊的dll文件,具有一定的用户界面和事件响应,而dll文件只是方法和属性的集合. 一.关于DLL的介绍 ...
- spring项目中使用定时任务
当我们希望在某个时间点来执行一些业务方法的时候就用到定时任务,在spring的项目中使用定时任务很简单.如下 第一步.加入jar包 <dependency> <groupId> ...
- XPATH 带命名空间数据的读取
在XML中,很多情况下有命名空间,如果直接使用XPATH 读取是会读到空节点. 解决办法如下: InputStream is=loader.getResourceAsStream("com/ ...
- Spring对事务的处理
1.加入spring-jdbc.jar包 2.配置数据源 3.配置spring事务管理器,spring的事务出现在业务层. <bean id="dataSource" cla ...
- POJ 2594 传递闭包的最小路径覆盖
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7171 Accepted: 2 ...
- UITableViewStyleGrouped 模式下 headview 多出一块高度问题
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView ...
- C++本质:类的赋值运算符=的重载,以及深拷贝和浅拷贝
关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符摘要: 在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作. 如果对象在申明的同时马上进行的初始化操作 ...
- java web 之 web.xml篇
web.xml文件 标签: 1. <web-app> 顶层标签,所有web.xml必须包含该标签.在该标签中,描述了当前Servlet版本以及其他一些信息. 2. <servlet& ...
- 类似github的框架
github是程序员经常上的网站,但如果是在一家苦逼不能访问外网的公司,那不能把自己的代码托管在github上绝对是一件非常痛苦的事情.如果想要在公司内网也可以用github托管自己的代码,那就要自己 ...