import java.util.ArrayList;
import java.util.List;
//打印一个字符串的全部子序列,包括空字符串
public class PrintAllSubsquences {
public static void printAllSubsquence(String str) {
char[] chs = str.toCharArray();
process(chs, 0);
}
//当前来到i位置
public static void process(char[] chs, int i) {
//如果i已经抵达终止位置,直接打印值
if (i == chs.length) {
System.out.println(String.valueOf(chs));
return;
}
//新增下一个字符进入考虑范围,并把之前的字符串也考虑进去
process(chs, i + 1);
char tmp = chs[i];
chs[i] = 0;
process(chs, i + 1);//不要当前字符的路
chs[i] = tmp;
}
public static void function(String str) {
char[] chs = str.toCharArray();
process(chs, 0, new ArrayList<Character>());
}
//当前来到i位置,要和不要,走两条路
// res 之前的选择,所形成的列表
public static void process(char[] chs, int i, List<Character> res) {
if(i == chs.length) {
printList(res); // 如果 i 来到了终止位置,打印之前的选择
return;
}
List<Character> resKeep = copyList(res);
resKeep.add(chs[i]);
process(chs, i+1, resKeep); // 要当前字符的路
List<Character> resNoInclude = copyList(res);
process(chs, i+1, resNoInclude); // 不要当前字符的路
}
public static void printList(List<Character> res) {
// ...;
}
public static List<Character> copyList(List<Character> list){
return null;
}
public static void main(String[] args) {
String test = "abc";
printAllSubsquence(test);
}
}
暴力递归——打印所有的子序列
最新推荐文章于 2024-06-17 23:55:50 发布