diff --git a/notes/38. 字符串的排列.md b/notes/38. 字符串的排列.md index 73de06ae..495f5870 100644 --- a/notes/38. 字符串的排列.md +++ b/notes/38. 字符串的排列.md @@ -8,6 +8,12 @@ ## 解题思路 +用回溯生成排列:每一层从还没用过的字符中选一个接到 s 后面,s 的长度等于字符串长度时得到一个排列,返回后撤销这次选择,再试下一个字符。 + +字符串可能有重复字符,例如 aab 中两个 a 交换位置得到的是同一个排列,直接回溯会输出重复的结果。先排序让相同的字符相邻,再规定相同的字符只能按从左到右的顺序使用:chars[i] 等于 chars[i - 1] 而 chars[i - 1] 还没被使用时,跳过 chars[i]。这样 aab 只生成 aab、aba、baa 各一次。排序后按顺序选字符,输出也就是字典序。 + +

+ ```java private List ret = new ArrayList<>(); diff --git a/notes/pics/offer38-dedup-tree.png b/notes/pics/offer38-dedup-tree.png new file mode 100644 index 00000000..62810087 Binary files /dev/null and b/notes/pics/offer38-dedup-tree.png differ