题解 | #字符串的排列#
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
每日简单代码,简短代码
import java.util.*; public class Solution { public ArrayList<String> Permutation(String str) { char[] arr = str.toCharArray(); Arrays.sort(arr); boolean[] st = new boolean[str.length()]; ArrayList<String> ans = new ArrayList<>(); dfs(arr, st, ans, ""); return ans; } private void dfs(char[] arr, boolean[] st, ArrayList<String> ans, String temp) { if (temp.length() == arr.length) { ans.add(temp); return; } HashSet<Character> set = new HashSet<>(); for (int i = 0; i < arr.length; i ++ ) { if (st[i]) continue; if (set.contains(arr[i])) continue; set.add(arr[i]); st[i] = true; dfs(arr, st, ans, temp + arr[i]); st[i] = false; } } }