JAVA 遞歸輸出所有可能的出棧序列


題目:用遞歸的方法輸出以ABCD入棧的所有可能且合法的出棧順序

方法借住二叉樹的思想構成:

public class Allorder {
    static void all_order(String in,String stack,String out)
    {
        if(in.equals("")&&stack.equals(""))
            System.out.println(out);
        if(!in.equals(""))
            all_order(in.substring(0,in.length()-1),stack+in.charAt(in.length()-1),out);
        if(!stack.equals(""))
            all_order(in,stack.substring(0,stack.length()-1),out+stack.charAt(stack.length()-1));
    }
    public static void main(String[] args) {
        String a=new String("ABCD");
        all_order(a,"","");
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM