《Cracking the Coding Interview》——第3章:栈和队列——题目2
2014-03-18 05:08
题目:实现一个栈,除了能进行push和pop之外,还能在O(1)时间内返回栈中最小的元素。
解法:用另一个“最小栈”存放最小的元素,每当有不小于当前最小值的元素进栈时,就代表最小值更新了(就算与当前最小值相等,也代表个数变了)。这时,同时要将最小值进栈。这个最小栈的栈顶就是最小的元素。出栈时,遇到数据栈的栈顶元素与最小栈相等时,要同时将最小栈出栈;否则只弹出数据栈即可。
代码:
// 3.2 Design a modified stack that in addition to Push and Pop can also provide minimum element present in the stack via Min function.
#include <climits>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std; class MinStack {
public:
bool empty() {
return st.empty();
} void push(int val) {
st.push(val);
if (min_st.empty() || val <= min_st.top()) {
min_st.push(val);
}
} void pop() {
if (st.empty()) {
return;
}
if (st.top() == min_st.top()) {
min_st.pop();
}
st.pop();
} int top() {
if (st.empty()) {
return INT_MIN;
}
return st.top();
} int min() {
if (st.empty()) {
return INT_MIN;
}
return min_st.top();
}
private:
stack<int> st;
stack<int> min_st;
}; int main()
{
char s[];
int op;
MinStack st; while (scanf("%s", s) == && strcmp(s, "END") != ) {
if (strcmp(s, "PUSH") == ) {
scanf("%d", &op);
st.push(op);
printf("push=%d\n", op);
} else if (strcmp(s, "POP") == ) {
op = st.top();
st.pop();
printf("pop=%d\n", op);
} else if (strcmp(s, "TOP") == ) {
printf("top=%d\n", st.top());
} else if (strcmp(s, "MIN") == ) {
printf("min=%d\n", st.min());
}
} return ;
}
《Cracking the Coding Interview》——第3章:栈和队列——题目2的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 数据结构(c语言版,严蔚敏)第3章栈和队列
第3章栈和队列
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 数据结构(C语言版)-第3章 栈和队列
3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- April 9 2017 Week 15 Sunday
In the evening one may praise the day. 入夜方能赞美白昼. I think that could be understand in different ways, ...
- SAP CRM WebClient UI和Fiori UI混搭并存
SAP CRM里有个功能可以创建HANA live report,消费HANA Studio里创建的模型. 最后创建好的report长这个样子: 具体创建步骤可以参考我的博客Step by Step ...
- 【转载】#402 - Value Equality vs. Reference Equality
When we normally think of "equality",we're thinking of value equality - the idea that the ...
- telegram汉化和代理
telegram在Ubuntu18.04的应用商店中可以一键下载. 1.注册:用国内手机号即可,就是验证码可能很慢. 2.汉化:关注zh-CN 频道,在点击其中的安装链接即可. 3.代理: 如果你使用 ...
- windows2003服务器双线双IP双网卡设置方法
双线双ip很好,网通用户访问网通线路,电信用户访问电信线路.但很多人会选用导入静态路由表,这个办法看似完美,其实问题很多. 1.电信用户如果被解析到网通的ip上,服务器根据路由表会返回电信线路,但用户 ...
- matlab 大块注释和取消注释的快捷键
matlab 大块注释和取消注释的快捷键 注释:Ctrl+R 取消注释:Ctrl +T
- ajax(form)图片上传(spring)
第一步:spring-web.xml <!--配置上传下载--> <bean id="multipartResolver" class="org.spr ...
- 截取前后缀FOR C
memcpy(new, old + prefix_len, sizeof(new)); memcpy(new, old, strlen(old) - suffix_len); :)
- cncert阅读报告
信息安全阅读报告 Problem 1: 国家计算机网络应急技术处理协调中心(简称“国家互联网应急中心”,英文缩写为“CNCERT”或“CNCERT/CC”)作为我国非政府层面网络安全应急体系核心技术协 ...
- 前端之jquery函数库
jquery介绍 jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. ...