LeetCode Min Stack 最小值栈
题意:实现栈的四个基本功能。要求:在get最小元素值时,复杂度O(1)。
思路:链表直接实现。最快竟然还要61ms,醉了。
class MinStack {
public:
MinStack(){
head.next=;
head.t=;
m=0x7FFFFFFF;
}
void push(int x) {
node *p=(node *)new(node);
p->t=x;
p->next=head.next;
head.next=p;
head.t++;
if(x < m) //要更新
m = x;
}
void pop() {
if(==head.t) return ;
else if(head.t)
{
head.t--;
if(head.next->t==m) //刚好等于最小值m,要更新最小值
{
int sma=0x7FFFFFFF;
node *p=head.next->next;
while( p )
{
if(p->t<sma)
sma=p->t;
p=p->next;
}
m=sma;
}
head.next=head.next->next;
}
}
int top() {
if(head.t)
return head.next->t;
return ;
}
int getMin() {
return m;
}
private:
struct node
{
int t;
node *next;
};
node head;
int m;
};
LeetCode Min Stack 最小值栈的更多相关文章
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- [LeetCode] Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- 利用html5看雪花飘落的效果
html5飘落的雪花堆积动画特效 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-T ...
- json字符串与json对象之间的转换
字符串转对象(strJSON代表json字符串) var obj = eval(strJSON); (运用时候需要除了eval()以外需要json.js包) var obj = strJSON. ...
- Java集合Set、Map、HashSet、HashMap、TreeSet、TreeMap等
1.Set和Map的关系: Set代表一种集合元素无序.不可重复的集合,Map代表一种由多个key-value对组成的集合. Set的集合继承体系: Map关系集合 Map集合的key特征:所有key ...
- Linux 之问题集锦(一)
1. 打开目录时,怎么只显示一个窗口 计算机 -- 编辑 -- 首选项 -- 行为 -- 总是总浏览器窗口中打开 2. linux中添加PATH时出现 Found a swap file by the ...
- 容易忘记的css属性和动画属性
动画属性 @keyframes 关键帧 --> animation 活泼 (配合使用) transform 变换 --> transition 过渡 (配合使用) 1.animation ...
- MFS安装
mfs github地址:https://github.com/moosefs/moosefs 一. 准备 1. 名字解释 Mfsmaster 元数据 Metalogger 元数据备份,用于恢复数据( ...
- Nacos深入浅出(三)
EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime())); 跟 ...
- Java 多线程高并发编程 笔记(二)
1. 单例模式(在内存之中永远只有一个对象) 1.1 多线程安全单例模式——不使用同步锁 public class Singleton { private static Singleton sin=n ...
- MyBatis入门Bug集锦X1
- python之is 和 == 的区别//编码和解码
一.is 和 == 的区别: 1 .id() 内存地址 2. == 比较 #比较两边的值 3. is 比较 #比较的是内存地址 数字,字符串,有小数据池 #数字小 ...