题解 | #字符串的排列# 递归法
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
import java.util.*; public class Solution { ArrayList<String> ans=new ArrayList<>(); boolean[]vis; public ArrayList<String> Permutation(String str) { char[]arr=str.toCharArray(); Arrays.sort(arr); vis=new boolean[arr.length]; dfs(arr,new StringBuilder()); return ans; } private void dfs(char[] arr, StringBuilder stringBuilder) { if(stringBuilder.length()==arr.length) { ans.add(stringBuilder.toString()); return; } for (int i = 0; i < arr.length; i++) { if(vis[i]) continue; if(i>0&&!vis[i-1]&&arr[i]==arr[i-1]) continue; vis[i]=true; stringBuilder.append(arr[i]); dfs(arr,stringBuilder); vis[i]=false; stringBuilder.deleteCharAt(stringBuilder.length()-1); } } }