转自:http://www.programcreek.com/2014/02/leetcode-min-stack-java/
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.
Thoughts
An array is a perfect fit for this problem. We can use a integer to track the top of the stack. You can use the Stack class from Java SDK, but I think a simple array is more efficient and more beautiful.
Java Solution
|
本文介绍如何设计一个支持 push、pop、top 和获取最小元素的常数时间复杂度的栈,通过使用数组实现,优化空间和时间效率。
701

被折叠的 条评论
为什么被折叠?



