重拾算法之剑指Offier——包含min函数的栈
2017-02-20 01:26
225 查看
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
import java.util.Stack; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { if (stack2.empty()){ stack2.push(node); }else { int top1 = stack1.peek(); if (top1 >= node){ stack2.push(node); }else { stack2.push(top1); } } stack1.push(node); } public void pop() { stack2.pop(); stack1.pop(); } public int top() { return stack1.peek(); } public int min() { return stack2.peek(); } }
相关文章推荐
- 剑指offer-算法题练习:part22 包含min函数的栈
- 重拾算法之剑指Offier——替换空格
- 重拾算法之剑指Offier——调整数组顺序使奇数位于偶数前面
- 剑指offer 包含min函数的栈
- 剑指offer 包含min函数的栈
- 面试算法(二十)包含min函数的栈
- (C++)剑指offer-20:包含min函数的栈(举例让抽象具体化)
- 【算法题目】包含min函数的栈
- 剑指offer代码解析——面试题21包含min函数的栈
- 剑指offer之面试题21包含min函数的栈
- [剑指offer]包含min函数的栈
- 【剑指offer】面试题30:包含min函数的栈
- 包含min函数的栈 -- 漫漫算法路 刷题篇
- 重拾算法之剑指Offier——从尾到头打印链表
- 重拾算法之剑指Offier——用两个栈实现队列
- 剑指offer:包含min函数的栈
- 剑指offer--面试题30:包含min函数的栈
- 剑指offer 21 包含min函数的栈
- 算法题3 设计包含min函数的栈
- 重拾算法之剑指Offier——栈的压入、弹出序列