剑指Offer22题栈的压入、弹出序列 java解答(如有错误欢迎批评指正)
来源:互联网 发布:java员工管理系统 编辑:程序博客网 时间:2024/05/20 14:25
package test;import java.util.Stack;public class IsPopOrder {public boolean isPopOrder(int[] push, int[] pop){boolean possible = false;if(push.length>0 && pop.length>0){int countPush = -1;int countPop = 0;int nextPush = 0;int nextPop = pop[countPop];Stack<Integer> stack = new Stack<>();while(countPop<pop.length){while(stack.isEmpty() || stack.peek() != nextPop){++countPush;if(countPush == push.length){break;}nextPush = push[countPush];stack.add(nextPush);}if(stack.peek() != nextPop){break;}stack.pop();++countPop;if(countPop != push.length){nextPop=pop[countPop];}else{break;}}if(stack.isEmpty() && nextPop==pop[pop.length-1])possible = true;}return possible;}public static void main(String[] args) {// TODO Auto-generated method stubint[] push = {1,2,3,4,5};int[] pop = {3,2,1,5,4};System.out.println(new IsPopOrder().isPopOrder(push, pop));}}
0 0
- 剑指Offer22题栈的压入、弹出序列 java解答(如有错误欢迎批评指正)
- 剑指Offer22栈的压入、弹出序列
- 剑指offer22--栈的压入弹出序列
- 剑指offer22:栈的压入、弹出序列
- 剑指Offer22:栈的压入、弹出序列
- 剑指offer22:栈的压入、弹出序列
- 将本地项目放到Github上(应该可以这么说)[如有错误欢迎大家批评指正]
- 各种猥琐的Java笔试/面试题目的答案(这是我自己测试得出的答案,如有不对的欢迎批评指正)
- 剑指Offer20题顺时针打印矩阵(欢迎批评指正)Java版
- 剑指Offer:栈的压入、弹出序列(JAVA)
- 剑指offer:栈的压入、弹出序列(java)
- java 栈的压入、弹出序列
- 小米公司编程题1(欢迎批评指正)
- 小米公司编程题2(欢迎批评指正)
- 剑指Offer: (Java实现) 栈的压入、弹出序列
- 栈---栈的压入、弹出序列(Java)
- 栈的压入、弹出序列(java版)
- 欢迎批评指正我的博客
- C++中类继承和虚函数
- Android-开源炫酷动画框架比较多
- font简写
- Spring事物管理——五种配置方法
- Windows系统下Python与NumPy的安装
- 剑指Offer22题栈的压入、弹出序列 java解答(如有错误欢迎批评指正)
- 参数关键字:params,ref,out
- C/C++中的类型别名关键字typedef
- Python2 urllib2 与 Python3 urllib.request API对照
- 剑指Offer20题顺时针打印矩阵(欢迎批评指正)Java版
- Medium 221题 Maximal Square
- 剑指Offer 19题 二叉树的镜像 Java版
- Leetcode 277. Find the Celebrity
- 剑指Offer 42题 翻转单词顺序 Java版