我可以在不展开堆栈的情况下从递归返回main吗?

时间:2015-06-01 18:20:44

标签: java recursion stack

我编写了一个递归方法来减少字符串的长度。如果他的字符串长度小于20,我想在我的扫描仪输入中尝试下一个字符串。有没有比我编码方式更有效的方法?

public static void main(String [] args){  
        try { 
            input = new Scanner(new FileInputStream("VectorKnots.txt"));
            output = new PrintWriter(new FileOutputStream("ReduceKnots.txt"));
        }catch (FileNotFoundException e){
            System.out.println("there is a problem with the file");
            System.exit(0);
        }
        while (input.hasNext()){
            loop2 = input.nextLine();
            loop1.delete(0, loop1.length());
            loop1.append(loop2);
            Triangle(loop1);
        }
    }
    public static void Triangle(StringBuilder loop1){
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("27")){
            trIndex = loop1.indexOf("27");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 4);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("72")){
            trIndex = loop1.indexOf("72");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 4);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("63")){
            trIndex = loop1.indexOf("63");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 5);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("63")){
            trIndex = loop1.indexOf("63");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 5);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("46")){
            trIndex = loop1.indexOf("46");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 2);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("64")){
            trIndex = loop1.indexOf("64");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 2);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("57")){
            trIndex = loop1.indexOf("57");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 3);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("75")){
            trIndex = loop1.indexOf("75");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 3);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("52")){
            trIndex = loop1.indexOf("52");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 6);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("25")){
            trIndex = loop1.indexOf("25");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 6);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("34")){
            trIndex = loop1.indexOf("34");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 7);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("43")){
            trIndex = loop1.indexOf("43");
            loop1.delete(trIndex, trIndex+1);
            loop1.insert(trIndex, 7);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("1") && loop1.indexOf("1") + 2) == 0){
            trIndex = loop1.indexOf("1");
            loop1.delete(trIndex);
            loop1.delete(trIndex + 1);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.toString().contains("0") && loop1.indexOf("1") + 2) == 1){
            trIndex = loop1.indexOf("0");
            loop1.delete(trIndex);
            loop1.delete(trIndex + 1);
            Triangle(loop1);
        }
        if (loop1.length() < 20){
            return;
        }
        if (loop1.length() > 19){
            out.println(loop1);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

如果使用迭代,则无需从深层嵌套递归返回。尝试:

public static void triangle(StringBuilder loop1) {
    for (;;) {
        if (loop1.length() < 20)
            return; 
        int trIndex = loop1.indexOf("27");
        if ( trIndex >= 0 ) {
            loop1.delete(trIndex+1);
            loop1.setCharAt(trIndex,'2');
            continue;   // start over using loop instead of recursion
        }

        trIndex = loop1.indexOf("72");
        if ( trIndex >= 0 ) {
            loop1.delete(trIndex+1);
            loop1.setCharAt(trIndex,'7');
            continue;   // start over using loop instead of recursion
        }

        // more cases

        break;
    }
}

另请注意

  1. 您最后的测试:if ( ... > 19 ) out.println(loop1)将无法执行任何操作,因为您之前有if ( ... < 20 ) return;;可能想将打印行移动到main ()
  2. contains()只是将indexOf()&gt; = 0,所以你也可以单独做,而不是两者兼而有。
  3. StringBuilder.insert(loc,int)将int转换为string,然后运行StringBuilder.insert的字符串版本;所以为什么不直接给它换一个字符串呢。
  4. 您可能会考虑构建某种自动机,也许只能查看字符串中每个字符一次。