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

栈和队列(一)

2019-11-08 01:56:05
字体:
来源:转载
供稿:网友

找出最小值

public class sAD01 { PRivate Stack<Integer> Data; private Stack<Integer> Min; public sAD01(){ this.Data = new Stack<Integer>(); this.Min = new Stack<Integer>(); } public void push(int newNum){ if(this.Min.isEmpty()){ this.Min.push(newNum); } else if(newNum <= this.getMin()){ this.Min.push(newNum); } this.Data.push(newNum); } public int pop(){ if(this.Data.isEmpty()){ throw new RuntimeException("your stack is empty"); } int value = this.Data.pop(); if(value == this.getMin()){ this.Min.pop(); } return value; } public int getMin(){ if(this.Min.isEmpty()){ throw new RuntimeException("your stack is empty"); } return this.Min.peek(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表