题目:
无重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合,字符串每个字符均不相同。
示例1:
输入:S = "qwe"
输出:["qwe", "qew", "wqe", "weq", "ewq", "eqw"]
示例2:
输入:S = "ab"
输出:["ab", "ba"]
代码实现:
class Solution {
List<String> list = new ArrayList<>();
public String[] permutation(String S) {
permutate(S.toCharArray(), 0);
String[] res = new String[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
return res;
}
public void permutate(char[] arr, int first) {
if (first == arr.length - 1) {
list.add(new String(arr));
return;
}
for (int i = first; i < arr.length; i++) {
swap(arr, first, i);
permutate(arr, first + 1);
swap(arr, first, i);
}
}
public void swap(char[] arr, int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}