Java - 使用递归从字符串创建所有子字符串 [英] Java - using recursion to create all substrings from a string

查看:734
本文介绍了Java - 使用递归从字符串创建所有子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的以下代码使用递归从字符串创建所有可能的子字符串。
我想知道有更好的编码方法吗?我想使用递归。

The following code in Java uses recursion to create all possible substrings from a string. I am wondering is there a better way of coding this? I want to use recursion.

public class main {

    public static void main(String[] args) {
        generate("hello");
    }

    public static void generate(String word) {
        if (word.length() == 1) {
            System.out.println(word);
            return;
        }else{
            System.out.println(word);
            generate(word.substring(0, word.length()-1)); 
            generate(word.substring(1, word.length())); 
        }

    }

}

FAQ
Q - 为什么我要使用递归来做这个?
A - 因为StackOverflow的首席执行官说递归非常重要
http:// www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

推荐答案

以下结果是最佳解决方案:

The following turned out to be the best solution:

public class recursive {

    static String in = "1234";

    public static void main(String[] args) {
        substrings(0,1);
    }

    static void substrings(int start, int end){
        if(start == in.length() && end == in.length()){
            return;
        }else{
            if(end == in.length()+1){
                substrings(start+1,start+1);
            }else{
                System.out.println(in.substring(start, end));
                substrings(start, end+1);
            }
        }
    }

}

它首先检查基本情况:如果start和end都等于in.length()。
因为如果它们是,那意味着没有更多的子串可以找到,程序结束。

It first checks the base case: if both start and end are equal to in.length(). Because if they are, that means there are no more substrings to be found, and the program ends.

让我们从start = 0开始,结束= 1 。它们显然不等于in.length(),并且end绝对不等于in.length()+ 1。
因此,子串(0,1)将被打印出来,即1.
子串的下一次迭代将是子串(0,2),并且in.substring(0,2)将是打印,这是12.这将一直持续到结束== in.length()+ 1,这发生在程序完成子串(0,4)并尝试移动到子串(0,5)时。
5 == in.length()+ 1,所以当发生这种情况时,程序将执行子串(start + 1,start + 1),这是子串(1,1)。当程序运行子串(2,2)时,该过程将继续使用子串(1,2)和(1,3),直到(1,5)。

Let's start with start=0 and end=1. They obviously don't equal in.length(), and end definitely doesn't equal in.length()+1. Thus, substring(0,1) will be printed out, which is 1. The next iteration of substrings will be substrings(0,2), and in.substring(0,2) will be printed, which is 12. This will continue until end == in.length()+1, which happens when the program finishes substrings(0,4) and tries to move on to substrings(0,5). 5 == in.length()+1, so when that happens, the program will do substrings(start+1,start+1), which is substrings(1,1). The process will continue with substrings(1,2), and (1,3), until (1,5) when the program will run substrings(2,2).

所有这些都将持续到子串(4,4),此时程序停止。

All of this will continue until substrings(4,4), which, at that point, the program stops.

结果如下所示:

1
12
123
1234

1 12 123 1234

2
23
234

2 23 234

3
34

3 34

4

这篇关于Java - 使用递归从字符串创建所有子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆