LeetCode (Min Stack)
来源:互联网 发布:java 二进制转字符串 编辑:程序博客网 时间:2024/05/21 08:39
Problem:
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.
Example:
MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.getMin(); --> Returns -3.minStack.pop();minStack.top(); --> Returns 0.minStack.getMin(); --> Returns -2.
Solution:class MinStack {private: stack<int> s1; stack<int> s2;public: /** initialize your data structure here. */ MinStack() { } void push(int x) { s1.push(x); if(s2.empty() || x <= getMin()) s2.push(x); } void pop() { if(s1.top() == getMin()) s2.pop(); s1.pop(); } int top() { return s1.top(); } int getMin() { return s2.top(); }};/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
阅读全文
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
- 566. Reshape the Matrix重塑矩阵
- 智神的诞生
- R语言写入excel表遇到的问题及解决
- 首份《顶级数据团队建设全景报告》重磅发布: 逾半数据团队称人才储备不足
- 未来币-win7安装
- LeetCode (Min Stack)
- [重磅,建议收藏]JAVA集合框架中的常用集合及其特点、适用场景、实现原理简介
- 【APP审核】发加速审核邮件
- css3 鼠标hover事件背景颜色渐变
- [经典]技术面试宝典: 很全面的算法和数据结构知识(含代码)
- DSP 面试部分试题
- NAT技术与代理服务器
- post请求
- ANSYS部分设置详解