155. Min Stack

class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
} void push(int x) {
if(s1.empty() && s2.empty()){
s1.push(x);
s2.push(x);
}
else{
if(x < s2.top()){
s1.push(x);
s2.push(x);
}
else{
s1.push(x);
s2.push(s2.top());
}
}
} void pop() {
s1.pop();
s2.pop();
return;
} int top() {
return s1.top();
} int getMin() {
return s2.top();
}
stack<int> s1,s2;
};

232. Implement Queue using Stacks

class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() { } /** Push element x to the back of queue. */
void push(int x) {
s1.push(x);
} /** Removes the element from in front of queue and returns that element. */
int pop() {
if(s2.empty())
shiftstack();
int tmp = s2.top();
s2.pop();
return tmp;
} /** Get the front element. */
int peek() {
if(s2.empty())
shiftstack();
return s2.top();
} /** Returns whether the queue is empty. */
bool empty() {
return s1.empty() && s2.empty() ? true : false;
} void shiftstack(){
if(s1.empty())
return;
while(!s1.empty()){
int tmp = s1.top();
s1.pop();
s2.push(tmp);
}
return;
} stack<int> s1,s2;
};

225. Implement Stack using Queues

将存储的队列之前的数值再次加入到队列的末尾

class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i = ;i < q.size() - ;i++){
int tmp = q.front();
q.pop();
q.push(tmp);
}
return;
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int tmp = q.front();
q.pop();
return tmp;
} /** Get the top element. */
int top() {
return q.front();
} /** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
queue<int> q;
};

http://www.cnblogs.com/grandyang/p/4568796.html

leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues的更多相关文章

  1. 【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( ...

  2. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  3. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  4. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  5. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  6. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  7. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  8. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  9. 【一天一道LeetCode】#232. Implement Queue using Stacks

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

随机推荐

  1. jquery - append prepend after before animate clearQueue stop

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Jedis 简单案例

    POM 依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> < ...

  3. 关于ARM CM3的启动文件分析

    下面以ARM Cortex_M3裸核的启动代码为例,做一下简单的分析.首先,在启动文件中完成了三项工作: 1.  堆栈以及堆的初始化 2.  定位中断向量表 3.  调用Reset Handler. ...

  4. jQuery如何判断input元素是否获得焦点(点击编辑时)

    问题提出 如果你要判断input元素是否获得焦点,或者是否处在活动编辑状态,使用jQuery的 hasFocus() 方法或 is(':focus') 方法貌似都无效!搜索网上给出的办法,几乎净是采用 ...

  5. js 时间转字符串,转成yyyy-MM-dd HH:mm:SS格式

    // 时间转字符串,转成yyyy-MM-dd HH:mm:SS格式 function dateToStr(datetime){ var dateTime = new Date(datetime); v ...

  6. MySQL 如何查看表的存储引擎

    MySQL 如何查看表的存储引擎   在MySQL中如何查看单个表的存储引擎? 如何查看整个数据库有那些表是某个特殊存储引擎,例如MyISAM存储引擎呢?下面简单的整理一下这方面的知识点. 如果要查看 ...

  7. [20181206]关于一致性读取3.txt

    [20181206]关于一致性读取3.txt --//简单演示一致性读取以及如何读取undo重构数据块的.我不想转储对应的undo块,解析那些复杂的过程. 1.环境:SCOTT@book> @ ...

  8. sqlserver中分区函数 partition by与 group by 区别 删除关键字段重复列

    partition  by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition  by ...

  9. Windows Server 2016-配置Windows Defender防病毒排除项

    Windows Server 2016 的计算机上的 Windows Defender 防病毒自动注册你在某些排除项,由你指定的服务器角色定义. 这些排除项不会显示在Windows 安全中心应用中所示 ...

  10. shell read变量的读入

    shell变量的输入: shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入获取,read为bash内置命令,可以通过help read查看帮助. 语法格式: read [参数 ...