题目
代码
class Solution {
public String[] permutation(String s) {
List<String> list = new ArrayList();
char[] arr = s.toCharArray();
StringBuilder sb = new StringBuilder();
boolean[] visited = new boolean[arr.length];
dfs(arr, "", visited, list);
return list.toArray(new String[0]);
}
public void dfs(char[] arr, String s, boolean[] visited, List list)
{
if(s.length() == arr.length)
{
list.add(s);
return;
}
for(int i=0; i<arr.length; i++)
{
if(visited[i]) continue;
visited[i] = true;
dfs(arr, s+String.valueOf(arr[i]), visited, list);
visited[i] = false;
}
}
}
结果
|