1栈和队列--设计一个有getMin功能的栈
来源:互联网 发布:精算 知乎 编辑:程序博客网 时间:2024/06/06 15:43
实现功能:
- 正常压栈
- 正常弹栈
- 返回栈中最小值
方法1:
public void push(int newNum) { if (this.stackMin.isEmpty()) { this.stackMin.push(newNum); } else if (newNum <= this.getmin()) { this.stackMin.push(newNum); } this.stackData.push(newNum); } public int pop() { if (this.stackData.isEmpty()) { throw new RuntimeException("Your stack is empty."); } int value = this.stackData.pop(); if (value == this.getmin()) { this.stackMin.pop(); } return value; } public int getmin() { if (this.stackMin.isEmpty()) { throw new RuntimeException("Your stack is empty."); } return this.stackMin.peek(); }
方法2:
public void push(int newNum) { if (this.stackMin.isEmpty()) { this.stackMin.push(newNum); } else if (newNum < this.getmin()) { this.stackMin.push(newNum); } else { int newMin = this.stackMin.peek(); this.stackMin.push(newMin); } this.stackData.push(newNum); } public int pop() { if (this.stackData.isEmpty()) { throw new RuntimeException("Your stack is empty."); } this.stackMin.pop(); return this.stackData.pop(); } public int getmin() { if (this.stackMin.isEmpty()) { throw new RuntimeException("Your stack is empty."); } return this.stackMin.peek(); } }
0 0
- 栈和队列 设计一个有getMin()功能的栈
- 栈和队列---设计一个有getMin功能的栈
- 1_1栈和队列_设计一个有Getmin功能的栈
- 1栈和队列--设计一个有getMin功能的栈
- 栈和队列之设计一个有getMin(得到最小值)功能的栈
- 栈和队列——设计一个有getMin功能的栈(一)
- 栈和队列——设计一个有getMin功能的栈(二)
- (一)栈和队列--设计一个有getMin功能的栈 java实现
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- Android中RecyclerView用法
- IIS 中部署 Asp.Net
- 信号量与互斥锁
- 数据库总结
- VirtualBox虚拟机迁移方法
- 1栈和队列--设计一个有getMin功能的栈
- 查看u3d模型的面数
- DataTable的用法小结
- unix网络编程(一)第一个例子
- Struts2学习
- 对于空间的理解
- ubuntu 14.04 配置android编译环境
- shell总结
- 团队在Github上协同开发项目流程