minStack
来源:互联网 发布:淘宝店头在线设计 编辑:程序博客网 时间:2024/06/06 00:32
Min Stack
用两个这样的栈可以实现一个再O(1)内获取最小元的队列。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.
Min Stack
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.
</pre><pre co
static class MinStack { final static int SIZE = 100000; int stk[] = new int[SIZE]; int top = 0; int min = Integer.MAX_VALUE; int nextMin[] = new int[SIZE]; public void push(int x) { if (x <= min) { min = x; nextMin[top] = top; } else { if (top - 1 >= 0) nextMin[top] = nextMin[top - 1]; } stk[top++] = x; } public void pop() { --top; if (stk[top] == min) { if (top - 1 >= 0) min = stk[nextMin[top - 1]]; } if(top==0){ min = Integer.MAX_VALUE; } } public int top() { if (top > 0) ; return stk[top - 1]; } public int getMin() { return min; } }
0 0
- MinStack
- MinStack
- minStack
- MinStack
- MinStack
- leetcode-MinStack
- LEETCODE-MinStack
- leetcode155.MinStack
- 【单调队列】leetcode MinStack
- LeedCode—MinStack
- LeetCode155 MinStack java题解
- leetcode 第160题 MinStack
- LeetCode题解:MinStack(三种解法)
- 算法-最小栈的实现-minStack
- MinStack + Implement Queue using stacks = MinQueue
- List<object>转换成IList<Word>
- Linux(redhat6)下的Python连接Oracle安装文档
- ACM hit 1010 The Angle
- FileInputStream/FileOutputStream的应用
- 临界区的实现原理
- minStack
- poj1323
- jquery操作select
- iOS开发--设置button选中状态
- 01-lucene简介和索引创建以及搜索
- 如何在Win7系统中批量更改文件名_以供程序批量读取文件
- Linux下查看系统信息命令
- Ubuntu下安装Code::Blocks
- mybatis中的#和$的区别