• [程序人生]: 判断字符串是否为出栈序列


    题目:

    给出一个字符串Initial和另一个字符串Detection,判断Detection是否为Initial的一个出栈序列

    如:Initial = 123456, Detection = 213654, 则Detection是Initial的一个出栈序列

    思路一:可以使用栈

    代码:

        public static boolean bStackOrder(String Initial, String Detection){
            if( Initial== null || Detection == null || Initial.length()==0 ||Detection.length() ==0 || Initial.length() != Detection.length() ){
                return false;
            }
    
            boolean bResult = false;
            
            char[] charInitial = Initial.toCharArray();
            char[] charDetection = Detection.toCharArray();     
            Stack<Character> StackTemp = new Stack<Character>();
            int iFalg = 0;
    
            for(int i = 0; i<charDetection.length; i++ ){
                if(iFalg <charInitial.length  &&  charDetection[i] == charInitial[iFalg]){  //需要检测的字符与原始字符下一个位置一样的话,则做一套出栈,入栈的操作,也就相当于临时栈不动,原始字符串的标志位后移一位
                    iFalg ++ ;
                }
                else{
                    if(StackTemp.size() == 0 || charDetection[i] != StackTemp.peek()){ //需要检测的字符是否与临时栈的栈顶一致,一致的话出战,不一致的话入栈
                        //临时栈先入栈到待检查字符的第一个位置
                        while(charInitial[iFalg] != charDetection[i]){
                            if(iFalg +1 < charInitial.length){   
                                StackTemp.push(charInitial[iFalg]);
                                iFalg ++;
                            }else{
                                System.out.println("111111111111111111111111111111111111");
                                return false; //待检查字符串的字符就不在原始字符中
                            }
                        }
                        
                        iFalg ++; //找到对应的字符, 则做一套出栈,入栈的操作,也就相当于临时栈不动,原始字符串的标志位后移一位
                    }
                    else{
                        //临时栈出栈
                        if(StackTemp.size() != 0){ //临时栈是否已经空了
                            System.out.println("Pop :" + StackTemp.peek());
                            StackTemp.pop();
                        }
                        else{
                            System.out.println("3333333333333333333333333333333333333");
                            return false;//临时栈是否已经空了,无法出栈,所以两个字符不匹配
                        }
                    }
                }
                
    
            }
            
            if(iFalg != charInitial.length || StackTemp.size() != 0){
                System.out.println("44444444444444444444444444444444444");
                System.out.println("iFalg :" + iFalg);    
                System.out.println("StackTemp.size :" + StackTemp.size());    
                return false;
            }
            else{
                return true;
            }
        }
  • 相关阅读:
    three.js 居中-模型
    three.js 打包为一个组-几个单独的模型
    ABP 菜单和权限
    set
    P2429 制杖题
    对线性筛的新理解
    P2817 宋荣子的城堡
    P2651 添加括号III
    P2858 [USACO06FEB]奶牛零食Treats for the Cows
    P1005 矩阵取数游戏
  • 原文地址:https://www.cnblogs.com/savageclc26/p/4918438.html
Copyright © 2020-2023  润新知