题目1522:包含min函数的栈
2015-03-10 12:33
169 查看
创建2个栈,一个存放题目数字,另一个把当前最小值压入栈中。
栈顶的就是最小值
栈顶的就是最小值
#include<cstdio> #include<cstring> #include<stack> using namespace std; stack<int>st1; stack<int>st2; int main() { int n; while(scanf("%d",&n) != EOF) { while(!st1.empty()) st1.pop(); while(!st2.empty()) st2.pop(); char ch[2]; while(n--) { scanf("%s",ch); if(ch[0] == 's') { int val; scanf("%d",&val); if(st2.empty() || st2.top() >= val) { st2.push(val); } st1.push(val); printf("%d\n",st2.top()); } else { if(st1.empty()) { printf("NULL\n"); } else { int val = st1.top(); st1.pop(); if(val == st2.top()) { st2.pop(); } if(st2.empty()) //这儿没判定,RE了3次,郁闷 { printf("NULL\n"); } else { printf("%d\n",st2.top()); } } } } } return 0; }
相关文章推荐
- 题目1522:包含min函数的栈
- 九度OJ-题目1522:包含min函数的栈
- 题目1522:包含min函数的栈
- 题目1522:包含min函数的栈
- 【剑指Offer面试编程题】题目1522:包含min函数的栈--九度OJ
- 【剑指Offer面试编程题】题目1522:包含min函数的栈--九度OJ
- 剑指Offer题目1522:包含min函数的栈
- 九度 题目1522:包含min函数的栈
- 【LeetCode】Min Stack && 【九度】题目1522:包含min函数的栈
- 题目1522:包含min函数的栈
- 题目1522:包含min函数的栈-九度
- 题目1522:包含min函数的栈
- 题目1522:包含min函数的栈
- 题目1522:包含min函数的栈
- 题目1522:包含min函数的栈
- 【九度】题目1522:包含min函数的栈
- 九度_题目1522:包含min函数的栈
- 九度题目1522——包含min函数的栈
- [Jobdu] 题目1522:包含min函数的栈
- 题目1522:包含min函数的栈