剑指offer 21. 包含min函数的栈
来源:互联网 发布:mac视频字幕制作软件 编辑:程序博客网 时间:2024/06/15 23:21
// 题目:写一个模拟栈的类,而且要多一个输出当前最小值的函数min()// 解法:加入最小数栈,将每次输入的数与栈顶数进行比较并进行相应的操作class myStack{private static int index = -1;//index表示当前指针private static int[] value = new int[999];//保存元素private static int[] minValue = new int[999];//保存最小值public void push(int input){index++;value[index] = input;if(index == 0){minValue[index] = input;}else{if(minValue[index-1]>input){//如果比当前值小则存入更新这个元素加入后的最小值minValue[index] = input;}else{minValue[index] = minValue[index-1];}}}public int peek(){if(index == -1){System.out.println("null");return -1;}return value[index];}public int min(){if(index == -1){System.out.println("null");return -1;}return minValue[index];}public int pop(){if(index == -1){System.out.println("null");return -1;}index--;return value[index+1];}public boolean isEmpty(){if(index == -1){return true;}else{return false;}}}public class Main {public static void main(String[] args) {myStack s = new myStack();s.push(1);System.out.println(s.pop());System.out.println(s.min());System.out.println(s.isEmpty());}}
0 0
- 【剑指offer】包含min函数的栈
- 剑指offer--包含min函数的栈
- 剑指Offer-->包含min函数的栈
- 《剑指offer》包含min函数的栈
- 剑指offer - 包含min函数的栈
- 剑指offer-包含min函数的栈
- 剑指offer:包含min函数的栈
- 包含min函数的栈|剑指offer
- 剑指offer-包含min函数的栈
- 剑指offer 包含min函数的栈
- 剑指offer:包含min函数的栈
- 剑指offer 包含min函数的栈
- [剑指offer]包含min函数的栈
- 《剑指offer》-包含min函数的栈
- 剑指offer----包含min函数的栈
- 【剑指offer】包含min函数的栈
- 剑指offer|包含min函数的栈
- 《剑指offer》包含min函数的栈
- Unity使用Socket与后台连接,包含Json的读写
- 划分dp,区间差最小
- 解决 win10 pycurl安装出错 Command "python setup.py egg_info" failed with error code 10
- 利用rsyslog 配置用户行为日志审计
- PAT_1043. Is It a Binary Search Tree
- 剑指offer 21. 包含min函数的栈
- Mybatis中接口和对应的mapper文件位置配置详解
- c语言:随机猜数字游戏
- Java 网络编程总结
- 用Imageview实现复选框效果
- 数据库事务隔离级别及相关实例
- 学习途径IOS
- 面向对象的特征有哪些方面
- C语言添加注释