题解 | #字符串的排列#经典回溯排列问题
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
import java.util.ArrayList;
import java.util.HashSet;
public class Solution {
ArrayList<String> result = new ArrayList<String>();
HashSet<String> set = new HashSet<String>();
StringBuilder sb = new StringBuilder();
boolean[] used;
public ArrayList<String> Permutation(String str) {
if (str == null || str.length() == 0) {
return result;
}
used = new boolean[str.length()];
helper(str);
for (String s : set) {
result.add(s);
}
return result;
}
private void helper(String str) {
if (sb.length() == str.length()) {
set.add(new String(sb).toString());
return;
}
for (int i = 0; i < str.length(); i++) {
if (used[i]) {
continue;
}
used[i] = true;
sb.append(str.charAt(i));
helper(str);
sb.deleteCharAt(sb.length() - 1);
used[i] = false;
}
}
}