以下Java代码使用递归来创建字符串的所有可能子字符串。
我想知道是否有更好的编码方式? 我想使用递归。
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()));
}
}
}
常见问题解答 问 - 为什么要使用递归? 答 - 因为 StackOverflow 的 CEO 表示递归很重要。 http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html