字符串的全部子序列(递归)
打印一个字符串的全部子序列, 包括空字符串
输入:
abc
输出:
// 第一个是空串
c
b
bc
a
ac
ab
abc
import java.io.BufferedInputStream;
import java.util.Scanner;
public class test {
public static void printAllSub(char[] str, int i, String res) {
if (i == str.length) {
System.out.println(res);
return ;
} else {
printAllSub(str, i + 1, res); // 不要下标为i+1的字符
printAllSub(str, i + 1, res+str[i]); // 要第i+1个字符
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
String str = cin.next();
printAllSub(str.toCharArray(), 0, "");
cin.close();
}
}========================================Talk is cheap, show me the code=======================================
CSDN博客地址:https://blog.csdn.net/qq_34115899
浙公网安备 33010602011771号