zoukankan      html  css  js  c++  java
  • [程序人生]: 判断字符串是否为出栈序列

    题目:

    给出一个字符串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;
            }
        }
  • 相关阅读:
    Netty源码分析——准备
    Netty入门
    Netty源码分析——EventLoopGroup建立
    三层架构搭建(asp.net mvc + ef)
    Springboot 1.5.x 集成基于Centos7的RabbitMQ集群安装及配置
    Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
    Springboot 2.0.x 引入链路跟踪Sleuth及Zipkin
    JAVA编码 —— 字符串关键字内容替换
    使用java发送QQ邮件的总结
    Docker原理探究
  • 原文地址:https://www.cnblogs.com/savageclc26/p/4918438.html
Copyright © 2011-2022 走看看