zoukankan      html  css  js  c++  java
  • 22:栈的压入、弹出序列

    import java.util.Stack;
    
    /**
     * 面试题22:栈的压入、弹出序列
     * 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。
     * 假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,
     * 但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
     */
    public class _22_stack_push_pop {
        public static void main(String[] args) {
            Solution22 solution22 = new Solution22();
            int[] a1 = {1, 2, 3, 4, 5};
            int[] a2 = {4, 5, 3, 2, 1};
            System.out.println(solution22.IsPopOrder(a1, a2));
        }
    }
    class Solution22 {
        public boolean IsPopOrder(int[] pushA, int[] popA) {
            if (pushA.length == 0 || popA.length == 0) {
                return false;
            }
            Stack<Integer> s1 = new Stack<Integer>();
            int pushLen = pushA.length;
            int popLen = popA.length;
            int curPushIndex = 0;
            int curPopIndex = 0;
            s1.push(pushA[curPushIndex++]);
            while (!s1.empty() && curPopIndex < popLen) { //栈不空且popA数组未遍历完
                if (popA[curPopIndex] == s1.peek()) {
                    s1.pop();
                    curPopIndex++;
                } else if (!s1.empty()&&curPushIndex==pushLen) { //跳出循环
                    break;
                } else if (curPushIndex < pushLen) { //不相等且push数组还有余值
                    s1.push(pushA[curPushIndex++]);
                }
                if (s1.empty() && curPushIndex < pushLen) {
                    s1.push(pushA[curPushIndex++]);
                }
            }
            if (s1.empty() && (curPopIndex == popLen)) {
                return true;
            }
            return false;
        }
    }
    
  • 相关阅读:
    硬件设计问题——持续更新
    PCB设计资源整理
    PCB层叠设计和电源分割
    PCB设计技巧
    铜厚电流、Layout 3W原则 20H原则 五五原则
    final关键字
    面向对象——继承与组合
    面向对象——继承
    this总结
    static总结
  • 原文地址:https://www.cnblogs.com/andy-zhou/p/6549449.html
Copyright © 2011-2022 走看看