您的位置:首页 > Web前端

剑指offer 28. 字符串的全排列

2017-05-05 16:55 267 查看
//题目:输入一个字符串,输出字符串的全排列
//解法:确定字符串的第一个字符,然后递归求之后字符的全排列
public class Main {

public static void main(String[] args) throws Exception {
printAllSeq("abc");
}

public static void printAllSeq(String str){
if(str == null){
return;
}
char[] input = str.toCharArray();
printAllSeqHelper(input,0);
}

public static void printAllSeqHelper(char[] input, int begin){
if(begin == input.length-1){
System.out.println(input);
}else{
for(int i = begin;i<input.length;i++){
char temp = input[begin];
input[begin] = input[i];
input[i] = temp;
printAllSeqHelper(input,begin+1);
temp = input[begin];
input[begin] = input[i];
input[i] = temp;
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: