Min Stack--LeetCode
来源:互联网 发布:appium ios java 编辑:程序博客网 时间:2024/06/18 07:55
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.
思路:使用向量存储元素,使用一个栈记录当前最小元素的索引,没压栈就是向向量存储元素,同时向栈中压入当前最小元素的索引。
#include <iostream>#include <stack>#include <vector>using namespace std;class Stack{ public: Stack() { } ~Stack() { } int getMin() { int index = minIndex.top(); return vec[index]; } void push(int x) { int index = minIndex.top(); vec.push_back(x); if(vec[index] <= x) minIndex.push(index); else minIndex.push(vec.size()-1); } void pop() { vec.pop_back(); minIndex.pop(); } int top() { return vec.back(); } private: vector<int> vec; stack<int> minIndex;}; int main(){ Stack st; return 0; }
0 0
- LeetCode[stack]: Min Stack
- [leetcode][stack] Min Stack
- 【Leetcode】Min Stack
- Leetcode: Min Stack
- leetcode Min stack
- Min Stack -- LeetCode
- Leetcode: Min Stack
- 【LeetCode】Min Stack
- leetcode Min Stack
- leetcode--Min Stack
- [Leetcode]Min stack
- leetcode:Min Stack
- leetcode 之 Min Stack
- leetcode Min Stack
- LeetCode(155) Min Stack
- [LeetCode]Min Stack
- leetcode:Min Stack
- leetcode - Min Stack
- 输入十个数,求出其中的最大值
- MySQL数据库表结构设计优化技巧总结 让你的表结构更加合理
- 6.PMAC下位机-下位机编程
- HelloCharts开源图表库(一)之折线图
- 安装Android的SDK
- Min Stack--LeetCode
- 淘汰算法LRU与LFU
- Servlet 3.0 新特性概述(3.1待总结)
- linux kernel 中断子系统之(一)-- ARM GIC 硬件
- Java5新增的同步工具
- 【C语言】求序列2/1+3/2+5/3+8/5+13/8+........前20项和。
- 矩阵乘法 之 strassen 算法
- MinHook源码阅读
- NetworkManagementService介绍