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() {
}
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的更多相关文章
- 【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(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【一天一道LeetCode】#232. Implement Queue using Stacks
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
随机推荐
- JavaScript 基础(三) - Date对象,RegExp对象,Math对象,Window 对象,History 对象,Location 对象,DOM 节点
Date对象 创建Date对象 //方法1:不指定参数 var date_obj = new Date(); alert(date_obj.toLocaleString()) //方法2:参数为日期字 ...
- css 两段对齐和超出部分...
.cont-detail ul li { display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-pack: j ...
- CSS之IE浏览器的hasLayout,IE低版本的bug根源
什么是hasLayout? hasLayout是IE特有的一个属性.很多的ie下的css bug都与其息息相关.在ie中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组 ...
- loj#6029. 「雅礼集训 2017 Day1」市场(线段树)
题意 链接 Sol 势能分析. 除法是不能打标记的,所以只能暴力递归.这里我们加一个剪枝:如果区间内最大最小值的改变量都相同的话,就变成区间减. 这样复杂度是\((n + mlogn) logV\)的 ...
- js替换字符中的斜杠反斜杠
var reg=/\\|\//g; var a="a\a\\a/b" alert(a.replace(reg,"-"));
- 图说OOP基础(一)
本文用图形化的形式描述OOP的相关知识.对OOP进行系统化的梳理,以便掌握,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: OOP的相关知识 OOP知识总图 [Object-Orientat ...
- c/c++ 标准库 map set 大锅炖
标准库 map set 大锅炖 一,关联容器有哪些 按关键字有序保存元素 map 保存key和value set 只保存key mulutimap key可以重复出现 multiset key可以重复 ...
- e lisp 自定义函数
自定义函数 (defun multi-by-seven (number) "multi number by seven" (interactive "p") ( ...
- strlen strcat strcpy strcmp 自己实现
strlen strcat strcpy strcmp 自己实现 strlen include <stdio.h> #include <string.h> #include & ...
- This network connection does not exist
This network connection does not exist 在windows server 2008上面map了一个磁盘,共享的folder被我停止共享后,点击该磁盘的disconn ...