LeetCode Min Stack
来源:互联网 发布:网络是利与弊 编辑:程序博客网 时间:2024/05/01 00:24
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.
最小栈的思路就是要用另外一个栈保存当前最小的元素,因此每个元素进栈之前都和最小栈的栈顶元素比较,比这个元素小就填最小的这个元素,否则还是填之前的最小元素。
class MinStack {public: stack<int> s1; stack<int> ms; void push(int x) { if(!s1.empty()) { int temp = ms.top(); if(temp>x) ms.push(x); else ms.push(temp); s1.push(x); }else { s1.push(x); ms.push(x); } } void pop() { s1.pop(); ms.pop(); } int top() { return s1.top(); } int getMin() { return ms.top(); }};
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
- 目标识别】深度学习进行目标识别的资源列表
- 解决IE浏览器下缓存不刷新问题。
- hadoop学习笔记
- 智慧city
- SetEvent与PulseEvent的区别
- LeetCode Min Stack
- Hive常用优化方法
- pl/sql中三种游标循环效率对比
- 白话一下什么是决策树模型
- C 文件夹遍历(Windows + Linux)跨平台
- self.navigationController一次性pop出两个页面
- KMP算法
- Linux修改系统时间命令
- ubuntu修改用户名、计算机名、主目录名