From 91036315d6310440d060bc3321c5a676c1d3ecd6 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Fri, 9 Dec 2022 12:47:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8feat:=20add=2093=E3=80=8195?= =?UTF-8?q?=E3=80=811775?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\272\214\345\217\211\346\240\221.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 20 +-- ...10\344\270\255\347\255\211\357\274\211.md" | 160 ++++++++++++++++++ ...10\344\270\255\347\255\211\357\274\211.md" | 4 +- ...10\344\270\255\347\255\211\357\274\211.md" | 152 +++++++++++++++++ ...10\344\270\255\347\255\211\357\274\211.md" | 120 +++++++++++++ 6 files changed, 445 insertions(+), 12 deletions(-) create mode 100644 "LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" create mode 100644 "LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" create mode 100644 "LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\344\272\214\345\217\211\346\240\221.md" "b/Index/\344\272\214\345\217\211\346\240\221.md" index 2b6a5843..39a86f32 100644 --- "a/Index/\344\272\214\345\217\211\346\240\221.md" +++ "b/Index/\344\272\214\345\217\211\346\240\221.md" @@ -1,5 +1,6 @@ | 题目 | 题解 | 难度 | 推荐指数 | | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- | +| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/solution/gong-shui-san-xie-yi-ti-san-jie-pai-xu-y-8uah/) | 中等 | 🤩🤩🤩🤩 | | [230. 二叉搜索树中第K小的元素](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/solution/gong-shui-san-xie-yi-ti-san-jie-pai-xu-y-8uah/) | 中等 | 🤩🤩🤩🤩 | | [240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-y1ns/) | 中等 | 🤩🤩🤩🤩🤩 | | [297. 二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/1741-1750/1743. \344\273\216\347\233\270\351\202\273\345\205\203\347\264\240\345\257\271\350\277\230\345\216\237\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1741-1750/1743. \344\273\216\347\233\270\351\202\273\345\205\203\347\264\240\345\257\271\350\277\230\345\216\237\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" index 0ceae484..3aee7270 100644 --- "a/LeetCode/1741-1750/1743. \344\273\216\347\233\270\351\202\273\345\205\203\347\264\240\345\257\271\350\277\230\345\216\237\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/1741-1750/1743. \344\273\216\347\233\270\351\202\273\345\205\203\347\264\240\345\257\271\350\277\230\345\216\237\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -6,13 +6,13 @@ Tag : 「哈希表」、「双指针」、「模拟」 -存在一个由 n 个不同元素组成的整数数组 nums ,但你已经记不清具体内容。好在你还记得 nums 中的每一对相邻元素。 +存在一个由 `n` 个不同元素组成的整数数组 `nums` ,但你已经记不清具体内容。好在你还记得 `nums` 中的每一对相邻元素。 -给你一个二维整数数组 adjacentPairs ,大小为 n - 1 ,其中每个 adjacentPairs[i] = [ui, vi] 表示元素 ui 和 vi 在 nums 中相邻。 +给你一个二维整数数组 `adjacentPairs` ,大小为 `n - 1` ,其中每个 $adjacentPairs[i] = [u_i, v_i]$ 表示元素 $u_i$ 和 $v_i$ 在 `nums` 中相邻。 -题目数据保证所有由元素 nums[i] 和 nums[i+1] 组成的相邻元素对都存在于 adjacentPairs 中,存在形式可能是 [nums[i], nums[i+1]] ,也可能是 [nums[i+1], nums[i]] 。这些相邻元素对可以 按任意顺序 出现。 +题目数据保证所有由元素 `nums[i]` 和 `nums[i+1]` 组成的相邻元素对都存在于 `adjacentPairs` 中,存在形式可能是 `[nums[i], nums[i+1]]` ,也可能是 `[nums[i+1], nums[i]]` 。这些相邻元素对可以 按任意顺序 出现。 -返回 原始数组 nums 。如果存在多种解答,返回 其中任意一个 即可。 +返回 原始数组 `nums` 。如果存在多种解答,返回 其中任意一个 即可。 示例 1: ``` @@ -40,12 +40,12 @@ Tag : 「哈希表」、「双指针」、「模拟」 ``` 提示: -* nums.length == n -* adjacentPairs.length == n - 1 -* adjacentPairs[i].length == 2 -* 2 <= n <= $10^5$ -* -$10^5$ <= nums[i], ui, vi <= $10^5$ -* 题目数据保证存在一些以 adjacentPairs 作为元素对的数组 +* $nums.length == n$ +* $adjacentPairs.length == n - 1$ +* $adjacentPairs[i].length == 2$ +* $2 <= n <= 10^5$ +* $-10^5 <= nums[i], u_i, v_i <= 10^5$ +* 题目数据保证存在一些以 `adjacentPairs` 作为元素对的数组 --- diff --git "a/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..5012d4cf --- /dev/null +++ "b/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,160 @@ +### 题目描述 + +这是 LeetCode 上的 **[1775. 通过最少操作次数使数组的和相等量]()** ,难度为 **中等**。 + +Tag : 「枚举」、「贪心」、「数学」 + + + +给你两个长度可能不等的整数数组 `nums1` 和 `nums2` 。两个数组中的所有值都在 `1` 到 `6` 之间(包含 `1` 和 `6`)。 + +每次操作中,你可以选择 任意 数组中的任意一个整数,将它变成 `1` 到 `6` 之间 任意 的值(包含 `1` 和 `6`)。 + +请你返回使 `nums1` 中所有数的和与 `nums2` 中所有数的和相等的最少操作次数。如果无法使两个数组的和相等,请返回 `-1` 。 + +示例 1: +``` +输入:nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2] + +输出:3 + +解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。 +- 将 nums2[0] 变为 6 。 nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2] 。 +- 将 nums1[5] 变为 1 。 nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2] 。 +- 将 nums1[2] 变为 2 。 nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2] 。 +``` +示例 2: +``` +输入:nums1 = [1,1,1,1,1,1,1], nums2 = [6] + +输出:-1 + +解释:没有办法减少 nums1 的和或者增加 nums2 的和使二者相等。 +``` +示例 3: +``` +输入:nums1 = [6,6], nums2 = [1] + +输出:3 + +解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。 +- 将 nums1[0] 变为 2 。 nums1 = [2,6], nums2 = [1] 。 +- 将 nums1[1] 变为 2 。 nums1 = [2,2], nums2 = [1] 。 +- 将 nums2[0] 变为 4 。 nums1 = [2,2], nums2 = [4] 。 +``` + +提示: +* $1 <= nums1.length, nums2.length <= 10^5$ +* $1 <= nums1[i], nums2[i] <= 6$ + +--- + +### 枚举 + 贪心 + 数学 + +令 `nums1` 的长度为 `n`,`nums2` 的长度为 `m`,根据题意两数组的值域分别为 $[n, 6n]$ 和 $[m, 6m]$,可分别视为数轴上的两条线段。 + +为了方便,我们人为固定 $n\leq m$,若不满足则交换两数组,返回 `minOperations(nums2, nums1)` 即可。 + +先来考虑无解的情况:当 $6n < m$ 时,说明两线段不重合,必然无法通过变换使得总和相等,直接返回 `-1`。 + +由于 $\max(n, m)$ 的范围为 $1e5$,且 $nums[i]$ 的值域大小 $C = 6$,因此我们可以通过枚举最终目标和 `x`(两线段的重合部分)来做,枚举范围不超过 $6 \times 1e5$。 + +于是问题转换为:**对于一个原总和为 `sum` 的数组 `nums` 而言,按照题目的变换规则,至少经过多少次变换,才能将其总和变为 `x`**。 + +根据原总和 `sum` 和目标结果 `x` 的大小关系进行分情况讨论(将两者差值绝对值记为 `d`): + +* 当 $sum < x$ 时,对于原数为 $nums[i]$ 的数而言,其能变为不超过 $nums[i] - 1$ 的任意数。 + + 例如 $6$ 能够变化为 $[1, 5]$ 中的任意数,即单个数值 $6$ 最多能够抵消 $6 - 1$ 个差值,不失一般性的可概括为原数为 $nums[i]$ 所能抵消的差值为 $nums[i] - 1$。 + + 因此,我们贪心的使用较大数进行变换(从 $6$ 往 $2$ 枚举 `i`),对于每个数值 `i` 而言,其所能提供的个数为 $\min(\left \lceil \frac{d}{i - 1} \right \rceil, cnst[i])$。 + +* 当 $sum > x$ 时,同理,原数为 $nums[i]$ 所能提供的最大抵消数为 $6 - nums[i]$,因此我们贪心使用较小数进行变换(从 $1$ 往 $5$ 枚举 `i`),对于每个数值 `i` 而言,其所能提供的个数为 $\min(\left \lceil \frac{d}{6 - i} \right \rceil, cnst[i])$。 + +如此一来,我们通过枚举两线段重合点 `x`,复杂度为 $O(C \times \max(n, m))$,并通过复杂度为 $O(C)$ 的数学方法来得知将两原数组总和变为 `x` 所需要的操作次数 `cnt`,在所有的 `cnt` 取最小值即是答案。整体计算量为 $3.6 \times 10^6$,可以过。 + + +Java 代码: +```Java +class Solution { + int[] c1 = new int[10], c2 = new int[10]; + int s1, s2; + public int minOperations(int[] nums1, int[] nums2) { + int n = nums1.length, m = nums2.length; + if (n > m) return minOperations(nums2, nums1); + if (m > 6 * n) return -1; + for (int x : nums1) { + c1[x]++; s1 += x; + } + for (int x : nums2) { + c2[x]++; s2 += x; + } + int ans = n + m; + for (int i = m; i <= 6 * n; i++) ans = Math.min(ans, getCnt(c1, s1, i) + getCnt(c2, s2, i)); + return ans; + } + int getCnt(int[] cnts, int sum, int x) { + int ans = 0; + if (sum > x) { + for (int i = 6, d = sum - x; i >= 2 && d > 0; i--) { + int c = Math.min((int) Math.ceil(d * 1.0 / (i - 1)), cnts[i]); + ans += c; d -= c * (i - 1); + } + } else if (sum < x) { + for (int i = 1, d = x - sum; i <= 5 && d > 0; i++) { + int c = Math.min((int) Math.ceil(d * 1.0 / (6 - i)), cnts[i]); + ans += c; d -= c * (6 - i); + } + } + return ans; + } +} +``` +Python 代码: +```Python +class Solution: + def minOperations(self, nums1: List[int], nums2: List[int]) -> int: + n, m = len(nums1), len(nums2) + if n > m: + return self.minOperations(nums2, nums1) + if m > 6 * n: + return -1 + c1, c2 = Counter(nums1), Counter(nums2) + s1, s2 = sum(nums1), sum(nums2) + def getCnt(cnts, tot, x): + ans = 0 + if tot > x: + d = tot - x + for i in range(6, 1, -1): + if d <= 0: + break + c = min(math.ceil(d / (i - 1)), cnts[i]) + ans, d = ans + c, d - c * (i - 1) + elif tot < x: + d = x - tot + for i in range(1, 6): + if d <= 0: + break + c = min(math.ceil(d / (6 - i)), cnts[i]) + ans, d = ans + c, d - c * (6 - i) + return ans + ans = n + m + for i in range(m, 6 * n + 1): + ans = min(ans, getCnt(c1, s1, i) + getCnt(c2, s2, i)) + return ans +``` +* 时间复杂度:$O(C \times \max(n, m) \times C)$,其中 $C = 6$ 为 $nums[i]$ 的值域大小 +* 空间复杂度:$O(C)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1775` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/411-420/413. \347\255\211\345\267\256\346\225\260\345\210\227\345\210\222\345\210\206\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/411-420/413. \347\255\211\345\267\256\346\225\260\345\210\227\345\210\222\345\210\206\357\274\210\344\270\255\347\255\211\357\274\211.md" index ad05c46b..8a0f01b2 100644 --- "a/LeetCode/411-420/413. \347\255\211\345\267\256\346\225\260\345\210\227\345\210\222\345\210\206\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/411-420/413. \347\255\211\345\267\256\346\225\260\345\210\227\345\210\222\345\210\206\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -31,8 +31,8 @@ Tag : 「双指针」、「模拟」、「数学」 ``` 提示: -* 1 <= nums.length <= 5000 -* -1000 <= nums[i] <= 1000 +* $1 <= nums.length <= 5000$ +* $-1000 <= nums[i] <= 1000$ --- diff --git "a/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..4922cfea --- /dev/null +++ "b/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,152 @@ +### 题目描述 + +这是 LeetCode 上的 **[93. 复原 IP 地址](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&tempkey=MTE5NF9PUlVickRVMVkveWdqQzNhSlJZeEVobW8yaXF3ZzV4U2NEMWpaQlI5MHN1cklxNFhNc3dCQlg0QnNpcXV1NGNqSG04X2dMTi1TN0RlUmpEd2YxaHJyLU00MTkxNEhtRi1kTmFoUnpicXFocW9oYnItU3BLU3RPS1pvODEtRXg2VkpyVWNCbFhaUmNKbnhnN21VN1JndzZwRG1WYlUwdGNYTW5KMTlBfn4%3D)** ,难度为 **中等**。 + +Tag : 「回溯」、「DFS」 + + + +有效 `IP` 地址 正好由四个整数(每个整数位于 `0` 到 `255` 之间组成,且不能含有前导 `0`),整数之间用 `'.'` 分隔。 + +例如:`"0.1.2.201"` 和 `"192.168.1.1"` 是 有效 `IP` 地址,但是 `"0.011.255.245"`、`"192.168.1.312"` 和 `"192.168@1.1"` 是 无效 `IP` 地址。 + +给定一个只包含数字的字符串 `s` ,用以表示一个 `IP` 地址,返回所有可能的有效 `IP` 地址,这些地址可以通过在 `s` 中插入 `'.'` 来形成。你 不能 重新排序或删除 `s` 中的任何数字。你可以按 任何 顺序返回答案。 + +示例 1: +``` +输入:s = "25525511135" + +输出:["255.255.11.135","255.255.111.35"] +``` +示例 2: +``` +输入:s = "0000" + +输出:["0.0.0.0"] +``` +示例 3: +``` +输入:s = "101023" + +输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] +``` + +提示: +* $1 <= s.length <= 20$ +* `s` 仅由数字组成 + +--- + +### 回溯算法 + +和 [131. 分割回文串](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247487047&idx=1&sn=117c48f20778868442fce44e100d2ea8) 一样,同样是一道求所有方案的题目,只能是没有太多优化的「爆搜」做法。 + +设计递归函数为 `void dfs(int idx, int n, List cur)`,其中 `idx` 和 `n` 分别代表当前处理字符串 `s` 的哪个位置,以及字符串 `s` 的总长度,而 `cur` 的则是代表子串 $s[0 ... (idx - 1)]$ 部分的具体划分方案。 + +用题目样例 `s = "25525511135"` 作为 🌰,`n` 固定为 `11`,当 `idx = 3` 时,`cur` 为 $s[0...2] = 255$ 部分的划分方案,`cur` 可能是 `[2,5,5]`、`[2,55]`、`[25,5]`、`[255]` 之一,在 `cur` 的基础上,我们继续爆搜剩余部分,即递归执行 `dfs(idx, n, cur)`,算法会将剩余部分的划分方案添加到 `cur` 上,我们只需要确保每次追加到 `cur` 的数值符合要求即可(没有前导零 且 范围在 $[0, 255]$ 中)。 + +在单次回溯过程中,我们可以将 `idx` 作为当前划分数字的左端点,通过枚举的形式找到右端点 `j`,并将当前数字 $s[idx ... (j - 1)]$ 加到 `cur` 中(若合法),回溯到底后再添加到 `cur` 的元素进行移除。 + +当 `idx = n` 代表整个 `s` 已经处理完成,若此时 `cur` 恰好有 $4$ 个元素,说明我们找到了一组合法方案,将其拼接成字符串追加到答案数组中。同时也是由于划分过程中 `cur` 最多只有 $4$ 个元素,我们可以用此做简单剪枝。 + +Java 代码: +```Java +class Solution { + List ans = new ArrayList<>(); + char[] cs; + public List restoreIpAddresses(String s) { + cs = s.toCharArray(); + dfs(0, cs.length, new ArrayList<>()); + return ans; + } + void dfs(int idx, int n, List cur) { + if (cur.size() > 4) return ; + if (idx == n) { + if (cur.size() == 4) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 4; i++) sb.append(cur.get(i)).append("."); + ans.add(sb.substring(0, sb.length() - 1)); + } + } else { + for (int i = idx; i < n; i++) { + int t = 0; + for (int j = idx; j <= i; j++) t = t * 10 + (cs[j] - '0'); + if (cs[idx] == '0' && i != idx) break; + if (t > 255) break; + cur.add(t); + dfs(i + 1, n, cur); + cur.remove(cur.size() - 1); + } + } + } +} +``` +Python 代码: +```Python +class Solution: + def restoreIpAddresses(self, s: str) -> List[str]: + ans = [] + def dfs(idx, n, cur): + if len(cur) > 4: + return + if idx == n: + if len(cur) == 4: + ans.append('.'.join(cur)) + else: + for i in range(idx, n): + t = 0 + for j in range(idx, i + 1): + t = t * 10 + (ord(s[j]) - ord('0')) + if s[idx] == '0' and i != idx: + break + if t > 255: + break + cur.append(str(t)) + dfs(i + 1, n, cur) + cur.pop() + dfs(0, len(s), []) + return ans +``` +TypeScript 代码: +```TypeScript +function restoreIpAddresses(s: string): string[] { + const ans = new Array() + function dfs(idx: number, n: number, cur: Array): void { + if (cur.length > 4) return + if (idx == n) { + if (cur.length == 4) { + let str = '' + for (let i = 0; i < 4; i++) str += cur[i] + "." + ans.push(str.substring(0, str.length - 1)) + } + } else { + for (let i = idx; i < n; i++) { + let t = 0 + for (let j = idx; j <= i; j++) t = t * 10 + (s.charCodeAt(j) - '0'.charCodeAt(0)) + if (s[idx] == '0' && i != idx) break + if (t > 255) break + cur.push(t) + dfs(i + 1, n, cur) + cur.pop() + } + } + } + dfs(0, s.length, new Array()) + return ans +} +``` +* 时间复杂度:爆搜不讨论时空复杂度 +* 空间复杂度:爆搜不讨论时空复杂度 + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.93` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..90c8fe83 --- /dev/null +++ "b/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,120 @@ +### 题目描述 + +这是 LeetCode 上的 **[95. 不同的二叉搜索树 II]()** ,难度为 **中等**。 + +Tag : 「树」、「二叉搜索树」、「BST」、「DFS」、「递归」、「爆搜」 + + + +给你一个整数 `n`,请你生成并返回所有由 `n` 个节点组成且节点值从 `1` 到 `n` 互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。 + +示例 1: +![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg) +``` +输入:n = 3 + +输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] +``` +示例 2: +``` +输入:n = 1 + +输出:[[1]] +``` + +提示: +* $1 <= n <= 8$ + +--- + +### 回溯算法 + +题目要我们求所有所能构造的二叉搜索树的具体方案,而给定 $n$ 个节点所能构成的二叉搜索树的个数为 [卡特兰数](https://baike.baidu.com/item/%E5%8D%A1%E7%89%B9%E5%85%B0%E6%95%B0)。 + +其他方案数同为卡特兰数的还包括:凸多边形三角划分、`n` 对括号正确匹配数目 ... + +回到本题,根据二叉搜索搜索的特性,若某个子树的根节点为 `root`,那么 `root` 的左子树任意节点值均比 `root.val` 要小,`root` 的右子树任意节点值均比 `root.val` 要大。 + +因此,假设我们使用 $[l, r]$ 连续段来构造二叉搜索树,并且选择了节点 `t` 作为二叉搜索树的根节点时,那么使用 $[l, t - 1]$ 构造出来的二叉搜索树均可作为根节点 $t$ 的左子树;同理,使用 $[t + 1, r]$ 构造出来的二叉搜索树均可作为根节点 `t` 的右子树。 + +也就是说,我们可以设计递归函数 `List dfs(int l, int r)`,含义为使用连续段 $[l, r]$ 进行二叉搜索树构造,并返回相应集合。 + +最终答案为 `dfs(1, n)`,起始我们可以枚举 $[1, n]$ 范围内的的每个数 `t` 作为根节点,并递归 `dfs(l, t - 1)` 和 `dfs(t + 1, r)` 获取左右子树的集合 `left` 和 `right`,结合「乘法原理」,枚举任意左子树和任意右子树,即可得到 `t` 作为根节点的二叉搜索树方案集,枚举所有 `t` 后即可得到所有二叉搜索树的总集。 + +> 注意:当我们运用乘法原理,来构造以 `t` 为根节点的二叉搜索树时,其 `left` 或 `right` 某一边可能为空集,但此时我们仍要将非空的一边子树进行挂载。 +> 为了确保两层新循环的逻辑会被执行,对于空集我们不能使用 `null` 来代指,而要使用 `[null]` 来代指。 + +Java 代码: +```Java +class Solution { + public List generateTrees(int n) { + return dfs(1, n); + } + List dfs(int l, int r) { + if (l > r) return new ArrayList<>(){{add(null);}}; + List ans = new ArrayList<>(); + for (int i = l; i <= r; i++) { + for (TreeNode x : dfs(l, i - 1)) { + for (TreeNode y : dfs(i + 1, r)) { + TreeNode root = new TreeNode(i); + root.left = x; root.right = y; + ans.add(root); + } + } + } + return ans; + } +} +``` +TypeScript 代码: +```TypeScript +function generateTrees(n: number): Array { + function dfs(l: number, r: number): Array { + if (l > r) return [null] + const ans = new Array() + for (let i = l; i <= r; i++) { + for (const x of dfs(l, i - 1)) { + for (const y of dfs(i + 1, r)) { + const root = new TreeNode(i) + root.left = x; root.right = y + ans.push(root) + } + } + } + return ans + } + return dfs(1, n) +} +``` +Python 代码: +```Python [] +class Solution: + def generateTrees(self, n: int) -> List[Optional[TreeNode]]: + def dfs(l, r): + if l > r: + return [None] + ans = [] + for i in range(l, r + 1): + for x in dfs(l, i - 1): + for y in dfs(i + 1, r): + root = TreeNode(i) + root.left, root.right = x, y + ans.append(root) + return ans + return dfs(1, n) +``` +* 时间复杂度:卡特兰数 +* 空间复杂度:卡特兰数 + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.95` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + From 75f3fcbe3bb941ad8c5a971c00ee75020ca205ae Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Fri, 9 Dec 2022 13:39:25 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8feat:=20add=2093=E3=80=8195?= =?UTF-8?q?=E3=80=811775?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Index/DFS.md | 1 + "Index/\344\272\214\345\217\211\346\240\221.md" | 2 +- "Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" | 1 + ...\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" | 2 +- ...\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" | 2 +- ...0\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" | 2 +- 6 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Index/DFS.md b/Index/DFS.md index c4721d28..602a6e13 100644 --- a/Index/DFS.md +++ b/Index/DFS.md @@ -5,6 +5,7 @@ | [37. 解数独](https://leetcode-cn.com/problems/sudoku-solver/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sudoku-solver/solution/he-n-huang-hou-yi-yang-shi-yi-dao-hui-su-lfpd/) | 困难 | 🤩🤩🤩🤩 | | [39. 组合总和](https://leetcode-cn.com/problems/combination-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum/solution/dfs-hui-su-suan-fa-yi-ji-ru-he-que-ding-wpbo5/) | 中等 | 🤩🤩🤩🤩 | | [40. 组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum-ii/solution/dfs-hui-su-jie-fa-yi-ji-ru-he-pan-duan-s-xlwy/) | 中等 | 🤩🤩🤩🤩 | +| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 | | [211. 添加与搜索单词 - 数据结构设计](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-we-un94/) | 中等 | 🤩🤩🤩🤩🤩 | | [241. 为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/different-ways-to-add-parentheses/solution/by-ac_oier-z07i/) | 中等 | 🤩🤩🤩🤩🤩 | | [282. 给表达式添加运算符](https://leetcode-cn.com/problems/expression-add-operators/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/expression-add-operators/solution/gong-shui-san-xie-hui-su-suan-fa-yun-yon-nl9z/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/Index/\344\272\214\345\217\211\346\240\221.md" "b/Index/\344\272\214\345\217\211\346\240\221.md" index 39a86f32..e3447d72 100644 --- "a/Index/\344\272\214\345\217\211\346\240\221.md" +++ "b/Index/\344\272\214\345\217\211\346\240\221.md" @@ -1,6 +1,6 @@ | 题目 | 题解 | 难度 | 推荐指数 | | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- | -| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/solution/gong-shui-san-xie-yi-ti-san-jie-pai-xu-y-8uah/) | 中等 | 🤩🤩🤩🤩 | +| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 | | [230. 二叉搜索树中第K小的元素](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/solution/gong-shui-san-xie-yi-ti-san-jie-pai-xu-y-8uah/) | 中等 | 🤩🤩🤩🤩 | | [240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-y1ns/) | 中等 | 🤩🤩🤩🤩🤩 | | [297. 二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" "b/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" index e6bd2b40..de15a954 100644 --- "a/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" +++ "b/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" @@ -5,6 +5,7 @@ | [39. 组合总和](https://leetcode-cn.com/problems/combination-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum/solution/dfs-hui-su-suan-fa-yi-ji-ru-he-que-ding-wpbo5/) | 中等 | 🤩🤩🤩🤩 | | [40. 组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum-ii/solution/dfs-hui-su-jie-fa-yi-ji-ru-he-pan-duan-s-xlwy/) | 中等 | 🤩🤩🤩🤩 | | [90. 子集 II](https://leetcode-cn.com/problems/subsets-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subsets-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-g77q/) | 中等 | 🤩🤩🤩🤩 | +| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 | | [131. 分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/palindrome-partitioning/solution/wei-sha-yao-zhe-yang-bao-sou-ya-shi-ru-h-41gf/) | 中等 | 🤩🤩🤩🤩 | | [212. 单词搜索 II](https://leetcode-cn.com/problems/word-search-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/word-search-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-am8f/) | 困难 | 🤩🤩🤩 | | [301. 删除无效的括号](https://leetcode-cn.com/problems/remove-invalid-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-invalid-parentheses/solution/yi-fen-zhong-nei-kan-dong-jiang-gua-hao-aya6k/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" index 5012d4cf..91a19cb5 100644 --- "a/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/1771-1780/1775. \351\200\232\350\277\207\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260\344\275\277\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -1,6 +1,6 @@ ### 题目描述 -这是 LeetCode 上的 **[1775. 通过最少操作次数使数组的和相等量]()** ,难度为 **中等**。 +这是 LeetCode 上的 **[1775. 通过最少操作次数使数组的和相等量](https://www.acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**。 Tag : 「枚举」、「贪心」、「数学」 diff --git "a/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" index 4922cfea..d0428fbb 100644 --- "a/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -1,6 +1,6 @@ ### 题目描述 -这是 LeetCode 上的 **[93. 复原 IP 地址](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&tempkey=MTE5NF9PUlVickRVMVkveWdqQzNhSlJZeEVobW8yaXF3ZzV4U2NEMWpaQlI5MHN1cklxNFhNc3dCQlg0QnNpcXV1NGNqSG04X2dMTi1TN0RlUmpEd2YxaHJyLU00MTkxNEhtRi1kTmFoUnpicXFocW9oYnItU3BLU3RPS1pvODEtRXg2VkpyVWNCbFhaUmNKbnhnN21VN1JndzZwRG1WYlUwdGNYTW5KMTlBfn4%3D)** ,难度为 **中等**。 +这是 LeetCode 上的 **[93. 复原 IP 地址](https://www.acoier.com/2022/12/09/93.%20%E5%A4%8D%E5%8E%9F%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**。 Tag : 「回溯」、「DFS」 diff --git "a/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" index 90c8fe83..d56238b2 100644 --- "a/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/91-100/95. \344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 II\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -1,6 +1,6 @@ ### 题目描述 -这是 LeetCode 上的 **[95. 不同的二叉搜索树 II]()** ,难度为 **中等**。 +这是 LeetCode 上的 **[95. 不同的二叉搜索树 II](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**。 Tag : 「树」、「二叉搜索树」、「BST」、「DFS」、「递归」、「爆搜」 From be35c5c3997fe6177d77b05fa089630f96d09723 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Fri, 9 Dec 2022 13:45:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8feat:=20update=20index?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Index/DFS.md | 1 + "Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" | 1 + "Index/\346\225\260\345\255\246.md" | 1 + "Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" | 1 + ...\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" | 2 +- 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Index/DFS.md b/Index/DFS.md index 602a6e13..ac29cd12 100644 --- a/Index/DFS.md +++ b/Index/DFS.md @@ -5,6 +5,7 @@ | [37. 解数独](https://leetcode-cn.com/problems/sudoku-solver/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sudoku-solver/solution/he-n-huang-hou-yi-yang-shi-yi-dao-hui-su-lfpd/) | 困难 | 🤩🤩🤩🤩 | | [39. 组合总和](https://leetcode-cn.com/problems/combination-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum/solution/dfs-hui-su-suan-fa-yi-ji-ru-he-que-ding-wpbo5/) | 中等 | 🤩🤩🤩🤩 | | [40. 组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum-ii/solution/dfs-hui-su-jie-fa-yi-ji-ru-he-pan-duan-s-xlwy/) | 中等 | 🤩🤩🤩🤩 | +| [93. 复原 IP 地址](https://leetcode.cn/problems/restore-ip-addresses/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/93.%20%E5%A4%8D%E5%8E%9F%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 | | [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 | | [211. 添加与搜索单词 - 数据结构设计](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-we-un94/) | 中等 | 🤩🤩🤩🤩🤩 | | [241. 为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/different-ways-to-add-parentheses/solution/by-ac_oier-z07i/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" "b/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" index de15a954..3144adc8 100644 --- "a/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" +++ "b/Index/\345\233\236\346\272\257\347\256\227\346\263\225.md" @@ -5,6 +5,7 @@ | [39. 组合总和](https://leetcode-cn.com/problems/combination-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum/solution/dfs-hui-su-suan-fa-yi-ji-ru-he-que-ding-wpbo5/) | 中等 | 🤩🤩🤩🤩 | | [40. 组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum-ii/solution/dfs-hui-su-jie-fa-yi-ji-ru-he-pan-duan-s-xlwy/) | 中等 | 🤩🤩🤩🤩 | | [90. 子集 II](https://leetcode-cn.com/problems/subsets-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subsets-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-g77q/) | 中等 | 🤩🤩🤩🤩 | +| [93. 复原 IP 地址](https://leetcode.cn/problems/restore-ip-addresses/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/93.%20%E5%A4%8D%E5%8E%9F%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 | | [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 | | [131. 分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/palindrome-partitioning/solution/wei-sha-yao-zhe-yang-bao-sou-ya-shi-ru-h-41gf/) | 中等 | 🤩🤩🤩🤩 | | [212. 单词搜索 II](https://leetcode-cn.com/problems/word-search-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/word-search-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-am8f/) | 困难 | 🤩🤩🤩 | diff --git "a/Index/\346\225\260\345\255\246.md" "b/Index/\346\225\260\345\255\246.md" index 208ed817..183d5a8c 100644 --- "a/Index/\346\225\260\345\255\246.md" +++ "b/Index/\346\225\260\345\255\246.md" @@ -79,6 +79,7 @@ | [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 | | [1734. 解码异或后的排列](https://leetcode-cn.com/problems/decode-xored-permutation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-permutation/solution/gong-shui-san-xie-note-bie-pian-li-yong-zeh6o/) | 中等 | 🤩🤩🤩🤩 | | [1738. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/solution/gong-shui-san-xie-xiang-jie-li-yong-er-w-ai0d/) | 中等 | 🤩🤩🤩 | +| [1775. 通过最少操作次数使数组的和相等](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 | | [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/by-ac_oier-wgr8/) | 中等 | 🤩🤩🤩🤩 | | [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 | | [面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" "b/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" index 9000c1e1..df978d77 100644 --- "a/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" +++ "b/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" @@ -39,6 +39,7 @@ | [1710. 卡车上的最大单元数](https://leetcode.cn/problems/maximum-units-on-a-truck/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-units-on-a-truck/solution/by-ac_oier-5wlo/) | 简单 | 🤩🤩🤩🤩🤩 | | [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/) | 困难 | 🤩🤩🤩🤩🤩 | | [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩🤩 | +| [1775. 通过最少操作次数使数组的和相等](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 | | [1833. 雪糕的最大数量](https://leetcode-cn.com/problems/maximum-ice-cream-bars/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-ice-cream-bars/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yrhjx/) | 中等 | 🤩🤩🤩🤩🤩 | | [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yh9qt/) | 中等 | 🤩🤩🤩🤩🤩 | | [1877. 数组中最大数对和的最小值](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ru29y/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" index d0428fbb..970ed27f 100644 --- "a/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/91-100/93. \345\244\215\345\216\237 IP \345\234\260\345\235\200\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -2,7 +2,7 @@ 这是 LeetCode 上的 **[93. 复原 IP 地址](https://www.acoier.com/2022/12/09/93.%20%E5%A4%8D%E5%8E%9F%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**。 -Tag : 「回溯」、「DFS」 +Tag : 「回溯算法」、「DFS」