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/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- lua调用c函数
参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...
- 「一入 Java 深似海 」系列课程
第一期 「一入 Java 深似海 」系列课程 - 第一期 第一节:Java 语言基础
- HDU - 6383 百度之星2018初赛B 1004 p1m2(二分答案)
p1m2 Accepts: 1003 Submissions: 4595 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072 ...
- 牛客多校3 C-Shuffle Cards(rope大法解决数组分块)
Shuffle Cards 链接:https://www.nowcoder.com/acm/contest/141/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26 ...
- 程序员必备,C#各类项目、开源项目插件资料收藏
一.AOP框架 Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种部署方面 ...
- JAVA获取本周 本月 本年 第一天和最后一天
/** * 日期工具类 */ public class DateUtils { /** * 获取今天 * @return String * */ public static String getTod ...
- [poj] Catch That Cow--bfs
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- python矩阵相加
举个栗子: # 两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵: # 使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中. import n ...
- JS 上拉加载
$(document).ready( function(){ $contentLoadTriggered = false; $("#mainDiv").scroll(functio ...
- SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
一.Mybatis框架 1.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...