题解 | #字符串排列#
字符串排列
http://www.nowcoder.com/practice/8380c8cf28954a188362206b1991b5d6
public ArrayList<String> getPermutation(String A) { // write code here ArrayList<String> res = new ArrayList<>(); if(A == null || A.length() == 0) return res; char[] chs = A.toCharArray(); dfs(chs, 0 , res); Collections.sort(res); Collections.reverse(res); return res; } public void dfs(char[] chs, int start, ArrayList<String> res){ if(start == chs.length){ res.add(new String(chs)); } for(int i = start ; i < chs.length; i ++){ swap(chs, i , start); dfs(chs, start + 1, res); swap(chs, i , start); } } public void swap(char[] arr , int i , int j ){ char tem = arr[i]; arr[i] = arr[j]; arr[j] = tem; }