首页 > 学院 > 开发设计 > 正文

剑指offer经典编程(十二)

2019-11-08 02:34:00
字体:
来源:转载
供稿:网友

包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

import java.util.Stack;public class Solution { Stack<Integer> dataStack = new Stack(); Stack<Integer> minStack = new Stack(); public void push(int node) { dataStack.push(node); if (minStack.size()==0||minStack.peek()>node){ minStack.push(node); }else { minStack.push(minStack.peek()); } } public void pop() { dataStack.pop(); minStack.pop(); } public int top() { return dataStack.peek(); } public int min() { return minStack.peek(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表