leetcode_题解_min stack
来源:互联网 发布:js获取屏幕高度 编辑:程序博客网 时间:2024/05/17 23:53
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
设置2个栈,一个存放数据,一个栈用栈顶存放当前的最小值,解法如下(没有考虑栈空之类的情况,OJ上反正AC了)
<pre name="code" class="cpp">class MinStack {public: void push(int x) { realStack.push(x); if(minStack.empty() || minStack.top()>=x)//注意realStack和minStack的元素个数不一定是相同的,如果用相同的那种方法,可能会MLE minStack.push(x); } void pop() { int topVal=realStack.top(); realStack.pop(); if(topVal<=minStack.top()) minStack.pop(); } int top() { return realStack.top(); } int getMin() { return minStack.top(); }private: stack<int> realStack,minStack;};
0 0
- leetcode_题解_min stack
- leetcode_题解_Surrounded Regions
- leetcode_题解_Clone Graph
- leetcode_题解_path sum_easy
- LeetCode_个人题解
- leetcode_题解_Pascal's Triangle_easy
- leetcode_题解_path sum 2
- leetcode_题解_Copy List with Random Pointer
- leetcode_题解_Pascal's Triangle II _easy
- leetcode_题解_Largest Rectangle in Histogram
- leetcode_题解_single number_小技巧
- leetcode_题解_Evaluate Reverse Polish Notation逆波兰式
- leetcode_题解_Merge Sorted Array _简单题
- leetcode_题解_Maximum Depth of Binary Tree_简单题
- leetcode_题解_Minimum Depth of Binary Tree _easy需细心
- 【Leetcode 题解】Min Stack
- LeetCode - Min Stack 题解
- LeetCode题解:Min Stack
- 日常问题笔记
- CMake入门指南
- poj 2508 Conic distance 计算几何
- 添加元素 下标操作
- js控制图片的缩放代码示例
- leetcode_题解_min stack
- BroadcastReceiver 有序广播
- Oracle 行列转换(存储过程)-mark
- Objective-C Runtime 运行时之一:类与对象
- ADT不会自动编译R文件,需要手动clean
- Webservice 调用方式整理
- java操作email发送邮件
- 对指定的PCAP包分析后,按照IP和PORT进行拆分PCAP
- windows下查看端口占用并结束进程