写一个函数,例如:给你的 ABC 则输出 ABC, ACB, BAC, BCA, CAB, CBA,此方法可以字符串不限制

public class ABCDpaixu {

public static void main(String[] args) {
	String s = "ABC";
	List<String> list = list(s, "");
	System.out.println(list);

}

//为了输出[ABC, ACB, BAC, BCA, CAB, CBA]
public static List<String> list(String base, String buff){
	List<String> result = new ArrayList<>();
	if(base.length()<=0) {
		result.add(buff);
	}
	for (int i = 0; i < base.length(); i++) {
		//new StringBuilder(base).deleteCharAt(i).toString()删除指定的元素
		List<String> temp=list(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i));
		result.addAll(temp);
	}
	return result;
}

}

猜你喜欢

转载自blog.csdn.net/k_x_sh/article/details/88686738