private char[] c;
private HashSet<String> set;
private int length;
public String[] permutation(String s) {
initial(s);
permute(0);
String[] res = new String[set.size()];
return set.toArray(res);
}
private void initial(String s) {
this.set = new HashSet<String>();
this.c = s.toCharArray();
this.length = c.length;
}
private void permute(int i) {
String temp = String.valueOf(c);
if (i == length - 1 && !set.contains(temp)) {
set.add(temp);
}
for (int j = i; j < this.length; ++j) {
swap(i, j);
permute(i + 1);
swap(i, j);
}
}
private void swap(int i, int j) {
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}
LeetCode-Algorithms-[Mid]面试题38. 字符串的排列
猜你喜欢
转载自blog.csdn.net/m0_37302219/article/details/105628412
今日推荐
周排行