diff --git a/Assets/Origins/Categories-List.md b/Assets/Origins/Categories-List.md index c55efaa0..916a55a7 100644 --- a/Assets/Origins/Categories-List.md +++ b/Assets/Origins/Categories-List.md @@ -75,11 +75,11 @@ #### 对撞指针题目 -###### 0167. 两数之和 II - 输入有序数组、0344. 反转字符串、0345. 反转字符串中的元音字母、0125. 验证回文串、0011. 盛最多水的容器、0611. 有效三角形的个数、0015. 三数之和、0016. 最接近的三数之和、0018. 四数之和、0259. 较小的三数之和、0658. 找到 K 个最接近的元素、1099. 小于 K 的两数之和、0075. 颜色分类、0360. 有序转化数组、0977. 有序数组的平方、0881. 救生艇、0042. 接雨水、剑指 Offer 21. 调整数组顺序使奇数位于偶数前面、0443. 压缩字符串 +###### 0167. 两数之和 II - 输入有序数组、0344. 反转字符串、0345. 反转字符串中的元音字母、0125. 验证回文串、0011. 盛最多水的容器、0611. 有效三角形的个数、0015. 三数之和、0016. 最接近的三数之和、0018. 四数之和、0259. 较小的三数之和、0658. 找到 K 个最接近的元素、1099. 小于 K 的两数之和、0075. 颜色分类、0360. 有序转化数组、0977. 有序数组的平方、0881. 救生艇、0042. 接雨水、0443. 压缩字符串 #### 快慢指针题目 -###### 0026. 删除有序数组中的重复项、0080. 删除有序数组中的重复项 II、0027. 移除元素、0283. 移动零、0845. 数组中的最长山脉、0088. 合并两个有序数组、0719. 找出第 K 小的数对距离、0334. 递增的三元子序列、0978. 最长湍流子数组 +###### 0026. 删除有序数组中的重复项、0080. 删除有序数组中的重复项 II、0027. 移除元素、0283. 移动零、0845. 数组中的最长山脉、0088. 合并两个有序数组、0719. 找出第 K 小的数对距离、0334. 递增的三元子序列、0978. 最长湍流子数组、剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 #### 分离双指针题目 diff --git a/Contents/00.Introduction/04.Solutions-List.md b/Contents/00.Introduction/04.Solutions-List.md index eb9af467..fe4b1814 100644 --- a/Contents/00.Introduction/04.Solutions-List.md +++ b/Contents/00.Introduction/04.Solutions-List.md @@ -1,4 +1,4 @@ -# LeetCode 题解(已完成 815 道) +# LeetCode 题解(已完成 818 道) | 题号 | 标题 | 题解 | 标签 | 难度 | | :------ | :------ | :------ | :------ | :------ | @@ -426,6 +426,7 @@ | 0803 | [打砖块](https://leetcode.cn/problems/bricks-falling-when-hit/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0803.%20%E6%89%93%E7%A0%96%E5%9D%97.md) | 并查集、数组、矩阵 | 困难 | | 0811 | [子域名访问计数](https://leetcode.cn/problems/subdomain-visit-count/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0811.%20%E5%AD%90%E5%9F%9F%E5%90%8D%E8%AE%BF%E9%97%AE%E8%AE%A1%E6%95%B0.md) | 数组、哈希表、字符串、计数 | 中等 | | 0814 | [二叉树剪枝](https://leetcode.cn/problems/binary-tree-pruning/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0814.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%AA%E6%9E%9D.md) | 树、深度优先搜索、二叉树 | 中等 | +| 0819 | [最常见的单词](https://leetcode.cn/problems/most-common-word/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0819.%20%E6%9C%80%E5%B8%B8%E8%A7%81%E7%9A%84%E5%8D%95%E8%AF%8D.md) | 哈希表、字符串、计数 | 简单 | | 0820 | [单词的压缩编码](https://leetcode.cn/problems/short-encoding-of-words/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0820.%20%E5%8D%95%E8%AF%8D%E7%9A%84%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81.md) | 字典树、数组、哈希表、字符串 | 中等 | | 0821 | [字符的最短距离](https://leetcode.cn/problems/shortest-distance-to-a-character/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0821.%20%E5%AD%97%E7%AC%A6%E7%9A%84%E6%9C%80%E7%9F%AD%E8%B7%9D%E7%A6%BB.md) | 数组、双指针、字符串 | 简单 | | 0824 | [山羊拉丁文](https://leetcode.cn/problems/goat-latin/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0824.%20%E5%B1%B1%E7%BE%8A%E6%8B%89%E4%B8%81%E6%96%87.md) | 字符串 | 简单 | @@ -569,6 +570,7 @@ | 1523 | [在区间范围内统计奇数数目](https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1523.%20%E5%9C%A8%E5%8C%BA%E9%97%B4%E8%8C%83%E5%9B%B4%E5%86%85%E7%BB%9F%E8%AE%A1%E5%A5%87%E6%95%B0%E6%95%B0%E7%9B%AE.md) | 数学 | 简单 | | 1534 | [统计好三元组](https://leetcode.cn/problems/count-good-triplets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1534.%20%E7%BB%9F%E8%AE%A1%E5%A5%BD%E4%B8%89%E5%85%83%E7%BB%84.md) | 数组、枚举 | 简单 | | 1547 | [切棍子的最小成本](https://leetcode.cn/problems/minimum-cost-to-cut-a-stick/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1547.%20%E5%88%87%E6%A3%8D%E5%AD%90%E7%9A%84%E6%9C%80%E5%B0%8F%E6%88%90%E6%9C%AC.md) | 数组、动态规划、排序 | 困难 | +| 1551 | [使数组中所有元素相等的最小操作数](https://leetcode.cn/problems/minimum-operations-to-make-array-equal/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1551.%20%E4%BD%BF%E6%95%B0%E7%BB%84%E4%B8%AD%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E7%9B%B8%E7%AD%89%E7%9A%84%E6%9C%80%E5%B0%8F%E6%93%8D%E4%BD%9C%E6%95%B0.md) | 数学 | 中等 | | 1556 | [千位分隔数](https://leetcode.cn/problems/thousand-separator/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1556.%20%E5%8D%83%E4%BD%8D%E5%88%86%E9%9A%94%E6%95%B0.md) | 字符串 | 简单 | | 1561 | [你可以获得的最大硬币数目](https://leetcode.cn/problems/maximum-number-of-coins-you-can-get/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1561.%20%E4%BD%A0%E5%8F%AF%E4%BB%A5%E8%8E%B7%E5%BE%97%E7%9A%84%E6%9C%80%E5%A4%A7%E7%A1%AC%E5%B8%81%E6%95%B0%E7%9B%AE.md) | 贪心、数组、数学、博弈、排序 | 中等 | | 1567 | [乘积为正数的最长子数组长度](https://leetcode.cn/problems/maximum-length-of-subarray-with-positive-product/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1567.%20%E4%B9%98%E7%A7%AF%E4%B8%BA%E6%AD%A3%E6%95%B0%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6.md) | 贪心、数组、动态规划 | 中等 | @@ -583,6 +585,7 @@ | 1695 | [删除子数组的最大得分](https://leetcode.cn/problems/maximum-erasure-value/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1695.%20%E5%88%A0%E9%99%A4%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BE%97%E5%88%86.md) | 数组、哈希表、滑动窗口 | 中等 | | 1698 | [字符串的不同子字符串个数](https://leetcode.cn/problems/number-of-distinct-substrings-in-a-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1698.%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E4%B8%8D%E5%90%8C%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AA%E6%95%B0.md) | 字典树、字符串、后缀数组、哈希函数、滚动哈希 | 中等 | | 1710 | [卡车上的最大单元数](https://leetcode.cn/problems/maximum-units-on-a-truck/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1710.%20%E5%8D%A1%E8%BD%A6%E4%B8%8A%E7%9A%84%E6%9C%80%E5%A4%A7%E5%8D%95%E5%85%83%E6%95%B0.md) | 贪心、数组、排序 | 简单 | +| 1716 | [计算力扣银行的钱](https://leetcode.cn/problems/calculate-money-in-leetcode-bank/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1716.%20%E8%AE%A1%E7%AE%97%E5%8A%9B%E6%89%A3%E9%93%B6%E8%A1%8C%E7%9A%84%E9%92%B1.md) | 数学 | 简单 | | 1720 | [解码异或后的数组](https://leetcode.cn/problems/decode-xored-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1720.%20%E8%A7%A3%E7%A0%81%E5%BC%82%E6%88%96%E5%90%8E%E7%9A%84%E6%95%B0%E7%BB%84.md) | 位运算、数组 | 简单 | | 1736 | [替换隐藏数字得到的最晚时间](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1736.%20%E6%9B%BF%E6%8D%A2%E9%9A%90%E8%97%8F%E6%95%B0%E5%AD%97%E5%BE%97%E5%88%B0%E7%9A%84%E6%9C%80%E6%99%9A%E6%97%B6%E9%97%B4.md) | 贪心、字符串 | 简单 | | 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1742.%20%E7%9B%92%E5%AD%90%E4%B8%AD%E5%B0%8F%E7%90%83%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 哈希表、数学、计数 | 简单 | diff --git a/Contents/00.Introduction/05.Categories-List.md b/Contents/00.Introduction/05.Categories-List.md index 8e7f72a7..8648025d 100644 --- a/Contents/00.Introduction/05.Categories-List.md +++ b/Contents/00.Introduction/05.Categories-List.md @@ -186,7 +186,6 @@ | 0977 | [有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0977.%20%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.md) | 数组、双指针、排序 | 简单 | | 0881 | [救生艇](https://leetcode.cn/problems/boats-to-save-people/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0881.%20%E6%95%91%E7%94%9F%E8%89%87.md) | 贪心、数组、双指针、排序 | 中等 | | 0042 | [接雨水](https://leetcode.cn/problems/trapping-rain-water/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0042.%20%E6%8E%A5%E9%9B%A8%E6%B0%B4.md) | 栈、数组、双指针、动态规划、单调栈 | 困难 | -| 剑指 Offer 21 | [调整数组顺序使奇数位于偶数前面](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E5%89%91%E6%8C%87%20Offer%2021.%20%E8%B0%83%E6%95%B4%E6%95%B0%E7%BB%84%E9%A1%BA%E5%BA%8F%E4%BD%BF%E5%A5%87%E6%95%B0%E4%BD%8D%E4%BA%8E%E5%81%B6%E6%95%B0%E5%89%8D%E9%9D%A2.md) | 数组、双指针、排序 | 简单 | | 0443 | [压缩字符串](https://leetcode.cn/problems/string-compression/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0443.%20%E5%8E%8B%E7%BC%A9%E5%AD%97%E7%AC%A6%E4%B8%B2.md) | 双指针、字符串 | 中等 | #### 快慢指针题目 @@ -202,6 +201,7 @@ | 0719 | [找出第 K 小的数对距离](https://leetcode.cn/problems/find-k-th-smallest-pair-distance/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0719.%20%E6%89%BE%E5%87%BA%E7%AC%AC%20K%20%E5%B0%8F%E7%9A%84%E6%95%B0%E5%AF%B9%E8%B7%9D%E7%A6%BB.md) | 数组、双指针、二分查找、排序 | 困难 | | 0334 | [递增的三元子序列](https://leetcode.cn/problems/increasing-triplet-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0334.%20%E9%80%92%E5%A2%9E%E7%9A%84%E4%B8%89%E5%85%83%E5%AD%90%E5%BA%8F%E5%88%97.md) | 贪心、数组 | 中等 | | 0978 | [最长湍流子数组](https://leetcode.cn/problems/longest-turbulent-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0978.%20%E6%9C%80%E9%95%BF%E6%B9%8D%E6%B5%81%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、动态规划、滑动窗口 | 中等 | +| 剑指 Offer 21 | [调整数组顺序使奇数位于偶数前面](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E5%89%91%E6%8C%87%20Offer%2021.%20%E8%B0%83%E6%95%B4%E6%95%B0%E7%BB%84%E9%A1%BA%E5%BA%8F%E4%BD%BF%E5%A5%87%E6%95%B0%E4%BD%8D%E4%BA%8E%E5%81%B6%E6%95%B0%E5%89%8D%E9%9D%A2.md) | 数组、双指针、排序 | 简单 | #### 分离双指针题目 diff --git a/Contents/01.Array/02.Array-Sort/01.Array-Bubble-Sort.md b/Contents/01.Array/02.Array-Sort/01.Array-Bubble-Sort.md index 0f3a4252..5d6e4c1b 100644 --- a/Contents/01.Array/02.Array-Sort/01.Array-Bubble-Sort.md +++ b/Contents/01.Array/02.Array-Sort/01.Array-Bubble-Sort.md @@ -53,7 +53,7 @@ class Solution: def bubbleSort(self, arr): # 第 i 趟排序 - for i in range(len(arr)): + for i in range(len(arr) - 1): # 从序列中前 n - i + 1 个元素的第 1 个元素开始,相邻两个元素进行比较 for j in range(len(arr) - i - 1): # 相邻两个元素进行比较,如果前者大于后者,则交换位置 diff --git a/Contents/01.Array/04.Array-Two-Pointers/01.Array-Two-Pointers.md b/Contents/01.Array/04.Array-Two-Pointers/01.Array-Two-Pointers.md index cbe3d326..1491c005 100644 --- a/Contents/01.Array/04.Array-Two-Pointers/01.Array-Two-Pointers.md +++ b/Contents/01.Array/04.Array-Two-Pointers/01.Array-Two-Pointers.md @@ -379,7 +379,7 @@ left_2 = 0 while left_1 < len(nums1) and left_2 < len(nums2): if 一定条件 1: left_1 += 1 - left_2 += 2 + left_2 += 1 elif 一定条件 2: left_1 += 1 elif 一定条件 3: diff --git a/Contents/01.Array/04.Array-Two-Pointers/02.Array-Two-Pointers-List.md b/Contents/01.Array/04.Array-Two-Pointers/02.Array-Two-Pointers-List.md index bd42c365..5626dc60 100644 --- a/Contents/01.Array/04.Array-Two-Pointers/02.Array-Two-Pointers-List.md +++ b/Contents/01.Array/04.Array-Two-Pointers/02.Array-Two-Pointers-List.md @@ -21,7 +21,6 @@ | 0977 | [有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0977.%20%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.md) | 数组、双指针、排序 | 简单 | | 0881 | [救生艇](https://leetcode.cn/problems/boats-to-save-people/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0881.%20%E6%95%91%E7%94%9F%E8%89%87.md) | 贪心、数组、双指针、排序 | 中等 | | 0042 | [接雨水](https://leetcode.cn/problems/trapping-rain-water/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0042.%20%E6%8E%A5%E9%9B%A8%E6%B0%B4.md) | 栈、数组、双指针、动态规划、单调栈 | 困难 | -| 剑指 Offer 21 | [调整数组顺序使奇数位于偶数前面](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E5%89%91%E6%8C%87%20Offer%2021.%20%E8%B0%83%E6%95%B4%E6%95%B0%E7%BB%84%E9%A1%BA%E5%BA%8F%E4%BD%BF%E5%A5%87%E6%95%B0%E4%BD%8D%E4%BA%8E%E5%81%B6%E6%95%B0%E5%89%8D%E9%9D%A2.md) | 数组、双指针、排序 | 简单 | | 0443 | [压缩字符串](https://leetcode.cn/problems/string-compression/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0443.%20%E5%8E%8B%E7%BC%A9%E5%AD%97%E7%AC%A6%E4%B8%B2.md) | 双指针、字符串 | 中等 | #### 快慢指针题目 @@ -37,6 +36,7 @@ | 0719 | [找出第 K 小的数对距离](https://leetcode.cn/problems/find-k-th-smallest-pair-distance/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0719.%20%E6%89%BE%E5%87%BA%E7%AC%AC%20K%20%E5%B0%8F%E7%9A%84%E6%95%B0%E5%AF%B9%E8%B7%9D%E7%A6%BB.md) | 数组、双指针、二分查找、排序 | 困难 | | 0334 | [递增的三元子序列](https://leetcode.cn/problems/increasing-triplet-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0334.%20%E9%80%92%E5%A2%9E%E7%9A%84%E4%B8%89%E5%85%83%E5%AD%90%E5%BA%8F%E5%88%97.md) | 贪心、数组 | 中等 | | 0978 | [最长湍流子数组](https://leetcode.cn/problems/longest-turbulent-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0978.%20%E6%9C%80%E9%95%BF%E6%B9%8D%E6%B5%81%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、动态规划、滑动窗口 | 中等 | +| 剑指 Offer 21 | [调整数组顺序使奇数位于偶数前面](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E5%89%91%E6%8C%87%20Offer%2021.%20%E8%B0%83%E6%95%B4%E6%95%B0%E7%BB%84%E9%A1%BA%E5%BA%8F%E4%BD%BF%E5%A5%87%E6%95%B0%E4%BD%8D%E4%BA%8E%E5%81%B6%E6%95%B0%E5%89%8D%E9%9D%A2.md) | 数组、双指针、排序 | 简单 | #### 分离双指针题目 diff --git a/Contents/02.Linked-List/02.Linked-List-Sort/01.Linked-List-Sort.md b/Contents/02.Linked-List/02.Linked-List-Sort/01.Linked-List-Sort.md index a1224bd3..4d6b233e 100644 --- a/Contents/02.Linked-List/02.Linked-List-Sort/01.Linked-List-Sort.md +++ b/Contents/02.Linked-List/02.Linked-List-Sort/01.Linked-List-Sort.md @@ -428,7 +428,7 @@ class Solution: # 计算桶的个数,并定义桶 bucket_count = (list_max - list_min) // bucket_size + 1 - buckets = [[] for _ in range(bucket_count)] + buckets = [None for _ in range(bucket_count)] # 将链表节点值依次添加到对应桶中 cur = head diff --git a/Contents/07.Tree/02.Binary-Search-Tree/01.Binary-Search-Tree.md b/Contents/07.Tree/02.Binary-Search-Tree/01.Binary-Search-Tree.md index 4527f538..455a4dd3 100644 --- a/Contents/07.Tree/02.Binary-Search-Tree/01.Binary-Search-Tree.md +++ b/Contents/07.Tree/02.Binary-Search-Tree/01.Binary-Search-Tree.md @@ -10,7 +10,7 @@  -二叉树具有一个特性,即:$左子树的节点值 < 根节点值 < 右子树的节点值$。 +二叉树具有一个特性,即:**左子树的节点值 < 根节点值 < 右子树的节点值**。 根据这个特性,如果我们以中序遍历的方式遍历整个二叉搜索树时,会得到一个递增序列。例如,一棵二叉搜索树的中序遍历序列如下图所示。 @@ -31,6 +31,12 @@ ### 2.2 二叉搜索树的查找代码实现 ```Python +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: if not root: @@ -67,6 +73,12 @@ class Solution: ### 3.2 二叉搜索树的插入代码实现 ```Python +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + class Solution: def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: if root == None: @@ -94,6 +106,12 @@ class Solution: ### 4.2 二叉搜索树的创建代码实现 ```Python +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + class Solution: def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: if root == None: @@ -139,6 +157,12 @@ class Solution: ### 5.2 二叉搜索树的删除代码实现 ```Python +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + class Solution: def deleteNode(self, root: TreeNode, val: int) -> TreeNode: if not root: diff --git a/README.md b/README.md index 1c656368..d3ba6f3f 100644 --- a/README.md +++ b/README.md @@ -259,4 +259,4 @@ - [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md) ## 11. 附加内容 -## [12. LeetCode 题解(已完成 815 道)](./Contents/00.Introduction/04.Solutions-List.md) \ No newline at end of file +## [12. LeetCode 题解(已完成 818 道)](./Contents/00.Introduction/04.Solutions-List.md) \ No newline at end of file diff --git "a/Solutions/0169. 345円244円232円346円225円260円345円205円203円347円264円240円.md" "b/Solutions/0169. 345円244円232円346円225円260円345円205円203円347円264円240円.md" index 46be0de2..35e44434 100644 --- "a/Solutions/0169. 345円244円232円346円225円260円345円205円203円347円264円240円.md" +++ "b/Solutions/0169. 345円244円232円346円225円260円345円205円203円347円264円240円.md" @@ -50,7 +50,7 @@ class Solution: numDict[num] += 1 else: numDict[num] = 1 - max = 0 + max = float('-inf') max_index = -1 for num in numDict: if numDict[num]> max: diff --git "a/Solutions/0199. 344円272円214円345円217円211円346円240円221円347円232円204円345円217円263円350円247円206円345円233円276円.md" "b/Solutions/0199. 344円272円214円345円217円211円346円240円221円347円232円204円345円217円263円350円247円206円345円233円276円.md" index bb83eeef..c5e955bc 100644 --- "a/Solutions/0199. 344円272円214円345円217円211円346円240円221円347円232円204円345円217円263円350円247円206円345円233円276円.md" +++ "b/Solutions/0199. 344円272円214円345円217円211円346円240円221円347円232円204円345円217円263円350円247円206円345円233円276円.md" @@ -48,16 +48,14 @@ class Solution: queue = [root] order = [] while queue: - level = [] size = len(queue) for i in range(size): curr = queue.pop(0) - level.append(curr.val) if curr.left: queue.append(curr.left) if curr.right: queue.append(curr.right) - if i == size-1: + if i == size - 1: order.append(curr.val) return order ``` diff --git "a/Solutions/0231. 2 347円232円204円345円271円202円.md" "b/Solutions/0231. 2 347円232円204円345円271円202円.md" index 360c14b0..7861522e 100644 --- "a/Solutions/0231. 2 347円232円204円345円271円202円.md" +++ "b/Solutions/0231. 2 347円232円204円345円271円202円.md" @@ -62,7 +62,7 @@ class Solution: 因为 $n$ 能取的最大值为 2ドル^{31}-1$。我们可以计算出:在 $n$ 的范围内,2ドル$ 的幂次方最大为 2ドル^{30} = 1073741824$。 -因为 2ドル$ 为质数,则 2ドル^{30}$ 的除数只有 2ドル^0, 2^1, ..., 2^{30}$。所以如果 $n$ 为 2ドル$ 的幂次方,则 $n$ 肯定能被 2ドル^{30}$ 整除,直接判断即可。 +因为 2ドル$ 为质数,则 2ドル^{30}$ 的除数只有 2ドル^0, 2^1, ..., 2^{30}$。所以如果 $n$ 为 2ドル$ 的幂次方,则 2ドル^{30}$ 肯定能被 $n$ 整除,直接判断即可。 ### 思路 2:代码 diff --git "a/Solutions/0289. 347円224円237円345円221円275円346円270円270円346円210円217円.md" "b/Solutions/0289. 347円224円237円345円221円275円346円270円270円346円210円217円.md" index c7df7fa9..fe971a84 100644 --- "a/Solutions/0289. 347円224円237円345円221円275円346円270円270円346円210円217円.md" +++ "b/Solutions/0289. 347円224円237円345円221円275円346円270円270円346円210円217円.md" @@ -5,36 +5,69 @@ ## 题目大意 -给定一个 `m * n` 的二维数组 `board`,每一个格子都可以看做是一个细胞。每个细胞都有一个初始状态:`1` 代表活细胞,`0` 代表死细胞。每个细胞与其相邻的八个位置(水平、垂直、对角线)细胞遵循以下生存规律: +**描述**:给定一个 $m \times n$ 大小的二维数组 $board,ドル每一个格子都可以看做是一个细胞。每个细胞都有一个初始状态:1ドル$ 代表活细胞,0ドル$ 代表死细胞。每个细胞与其相邻的八个位置(水平、垂直、对角线)细胞遵循以下生存规律: -- 如果活细胞周围八个位置的活细胞数少于 2 个,则该位置活细胞死亡; -- 如果活细胞周围八个位置有 2 个或 3 个活细胞,则该位置活细胞仍然存活; -- 如果活细胞周围八个位置有超过 3 个活细胞,则该位置活细胞死亡; -- 如果死细胞周围正好有 3 个活细胞,则该位置死细胞复活。 +- 如果活细胞周围八个位置的活细胞数少于 2ドル$ 个,则该位置活细胞死亡; +- 如果活细胞周围八个位置有 2ドル$ 个或 3ドル$ 个活细胞,则该位置活细胞仍然存活; +- 如果活细胞周围八个位置有超过 3ドル$ 个活细胞,则该位置活细胞死亡; +- 如果死细胞周围正好有 3ドル$ 个活细胞,则该位置死细胞复活。 二维数组代表的下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的的。其中细胞的出生和死亡是同时发生的。 -现在给定 `m * n` 的二维数组 `board` 的当前状态。 +现在给定 $m \times n$ 的二维数组 $board$ 的当前状态。 -要求:返回下一个状态。 +**要求**:返回下一个状态。 + +**说明**: + +- $m == board.length$。 +- $n == board[i].length$。 +- 1ドル \le m, n \le 25$。 +- $board[i][j]$ 为 0ドル$ 或 1ドル$。 +- **进阶**: + - 你可以使用原地算法解决本题吗?请注意,面板上所有格子需要同时被更新:你不能先更新某些格子,然后使用它们的更新后的值再更新其他格子。 + - 本题中,我们使用二维数组来表示面板。原则上,面板是无限的,但当活细胞侵占了面板边界时会造成问题。你将如何解决这些问题? + + +**示例**: + +- 示例 1: + + + +```Python +输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] +输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]] +``` + +- 示例 2: + + + +```Python +输入:board = [[1,1],[1,0]] +输出:[[1,1],[1,1]] +``` ## 解题思路 +### 思路 1:模拟 + 因为下一个状态隐含了过去细胞的状态,所以不能直接在原二维数组上直接进行修改。细胞的状态总共有四种情况: -- 死细胞 -> 死细胞,即 `0` -> `0`。 -- 死细胞 -> 活细胞,即 `0` -> `1`。 -- 活细胞 -> 活细胞,即 `1` -> `1`。 -- 活细胞 -> 死细胞,即 `1` -> `0`。 +- 死细胞 -> 死细胞,即 0ドル \rightarrow 0$。 +- 死细胞 -> 活细胞,即 0ドル \rightarrow 1$。 +- 活细胞 -> 活细胞,即 1ドル \rightarrow 1$。 +- 活细胞 -> 死细胞,即 1ドル \rightarrow 0$。 -死细胞 -> 死细胞,活细胞 -> 活细胞,不会对前后状态造成影响,所以主要考虑另外两种情况。我们把活细胞 -> 死细胞暂时标记为 `-1`,并且统计每个细胞周围活细胞数量时,使用绝对值统计,这样 `abs(-1)` 也可以暂时标记为活细胞。然后把死细胞 -> 活细胞暂时标记为 `2`,这样判断的时候也不会统计上去。然后开始遍历。 +死细胞 -> 死细胞,活细胞 -> 活细胞,不会对前后状态造成影响,所以主要考虑另外两种情况。我们把活细胞 -> 死细胞暂时标记为 $-1,ドル并且统计每个细胞周围活细胞数量时,使用绝对值统计,这样 $abs(-1)$ 也可以暂时标记为活细胞。然后把死细胞 -> 活细胞暂时标记为 2ドル,ドル这样判断的时候也不会统计上去。然后开始遍历。 - 遍历二维数组的每一个位置。并对该位置遍历周围八个位置,计算出八个位置上的活细胞数量。 - - 如果此位置是活细胞,并且周围活细胞少于 `2` 个或超过 `3` 个,则将其暂时标记为 `-1`,意为此细胞死亡。 - - 如果此位置是死细胞,并且周围有 3 个活细胞,则将暂时标记为 `1`,意为此细胞复活。 -- 遍历完之后,再次遍历一遍二维数组,如果该位置为 `-1`,将其赋值为 `0`,如果该位置为 `2`,将其赋值为 `1`。 + - 如果此位置是活细胞,并且周围活细胞少于 2ドル$ 个或超过 3ドル$ 个,则将其暂时标记为 $-1,ドル意为此细胞死亡。 + - 如果此位置是死细胞,并且周围有 3ドル$ 个活细胞,则将暂时标记为 2ドル,ドル意为此细胞复活。 +- 遍历完之后,再次遍历一遍二维数组,如果该位置为 $-1,ドル将其赋值为 0ドル,ドル如果该位置为 2ドル,ドル将其赋值为 1ドル$。 -## 代码 +### 思路 1:代码 ```Python class Solution: @@ -69,3 +102,8 @@ class Solution: board[row][col] = 1 ``` +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(m \times n),ドル其中 $m$、$n$ 分别为 $board$ 的行数和列数。 +- **空间复杂度**:$O(m \times n)$。 + diff --git "a/Solutions/0819. 346円234円200円345円270円270円350円247円201円347円232円204円345円215円225円350円257円215円.md" "b/Solutions/0819. 346円234円200円345円270円270円350円247円201円347円232円204円345円215円225円350円257円215円.md" new file mode 100644 index 00000000..bcadabf1 --- /dev/null +++ "b/Solutions/0819. 346円234円200円345円270円270円350円247円201円347円232円204円345円215円225円350円257円215円.md" @@ -0,0 +1,91 @@ +# [0819. 最常见的单词](https://leetcode.cn/problems/most-common-word/) + +- 标签:哈希表、字符串、计数 +- 难度:简单 + +## 题目大意 + +**描述**:给定一个字符串 $paragraph$ 表示段落,再给定搞一个禁用单词列表 $banned$。 + +**要求**:返回出现次数最多,同时不在禁用列表中的单词。 + +**说明**: + +- 题目保证至少有一个词不在禁用列表中,而且答案唯一。 +- 禁用列表 $banned$ 中的单词用小写字母表示,不含标点符号。 +- 段落 $paragraph$ 只包含字母、空格和下列标点符号`!?',;.` +- 段落中的单词不区分大小写。 +- 1ドル \le 段落长度 \le 1000$。 +- 0ドル \le 禁用单词个数 \le 100$。 +- 1ドル \le 禁用单词长度 \le 10$。 +- 答案是唯一的,且都是小写字母(即使在 $paragraph$ 里是大写的,即使是一些特定的名词,答案都是小写的)。 +- 不存在没有连字符或者带有连字符的单词。 +- 单词里只包含字母,不会出现省略号或者其他标点符号。 + +**示例**: + +- 示例 1: + +```Python +输入: +paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." +banned = ["hit"] +输出: "ball" +解释: +"hit" 出现了3次,但它是一个禁用的单词。 +"ball" 出现了2次 (同时没有其他单词出现2次),所以它是段落里出现次数最多的,且不在禁用列表中的单词。 +注意,所有这些单词在段落里不区分大小写,标点符号需要忽略(即使是紧挨着单词也忽略, 比如 "ball,"), +"hit"不是最终的答案,虽然它出现次数更多,但它在禁用单词列表中。 +``` + +- 示例 2: + +```Python +输入: +paragraph = "a." +banned = [] +输出:"a" +``` + +## 解题思路 + +### 思路 1:哈希表 + +1. 将禁用词列表转为集合 $banned\underline{}set$。 +2. 遍历段落 $paragraph,ドル获取段落中的所有单词。 +3. 判断当前单词是否在禁用词集合中,如果不在禁用词集合中,则使用哈希表对该单词进行计数。 +4. 遍历完,找出哈希表中频率最大的单词,将该单词作为答案进行返回。 + +### 思路 1:代码 + +```Python +class Solution: + def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: + banned_set = set(banned) + cnts = Counter() + + word = "" + for ch in paragraph: + if ch.isalpha(): + word += ch.lower() + else: + if word and word not in banned_set: + cnts[word] += 1 + word = "" + if word and word not in banned_set: + cnts[word] += 1 + + max_cnt, ans = 0, "" + for word, cnt in cnts.items(): + if cnt> max_cnt: + max_cnt = cnt + ans = word + + return ans +``` + +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n + m),ドル其中 $n$ 为段落 $paragraph$ 的长度,$m$ 是禁用词 $banned$ 的长度。 +- **空间复杂度**:$O(n + m)$。 + diff --git "a/Solutions/0912. 346円216円222円345円272円217円346円225円260円347円273円204円.md" "b/Solutions/0912. 346円216円222円345円272円217円346円225円260円347円273円204円.md" index 14665a64..920f794c 100644 --- "a/Solutions/0912. 346円216円222円345円272円217円346円225円260円347円273円204円.md" +++ "b/Solutions/0912. 346円216円222円345円272円217円346円225円260円347円273円204円.md" @@ -51,7 +51,7 @@ class Solution: def bubbleSort(self, arr): # 第 i 趟排序 - for i in range(len(arr)): + for i in range(len(arr) - 1): # 从序列中前 n - i + 1 个元素的第 1 个元素开始,相邻两个元素进行比较 for j in range(len(arr) - i - 1): # 相邻两个元素进行比较,如果前者大于后者,则交换位置 diff --git "a/Solutions/1551. 344円275円277円346円225円260円347円273円204円344円270円255円346円211円200円346円234円211円345円205円203円347円264円240円347円233円270円347円255円211円347円232円204円346円234円200円345円260円217円346円223円215円344円275円234円346円225円260円.md" "b/Solutions/1551. 344円275円277円346円225円260円347円273円204円344円270円255円346円211円200円346円234円211円345円205円203円347円264円240円347円233円270円347円255円211円347円232円204円346円234円200円345円260円217円346円223円215円344円275円234円346円225円260円.md" new file mode 100644 index 00000000..4603d73c --- /dev/null +++ "b/Solutions/1551. 344円275円277円346円225円260円347円273円204円344円270円255円346円211円200円346円234円211円345円205円203円347円264円240円347円233円270円347円255円211円347円232円204円346円234円200円345円260円217円346円223円215円344円275円234円346円225円260円.md" @@ -0,0 +1,81 @@ +# [1551. 使数组中所有元素相等的最小操作数](https://leetcode.cn/problems/minimum-operations-to-make-array-equal/) + +- 标签:数学 +- 难度:中等 + +## 题目大意 + +**描述**:存在一个长度为 $n$ 的数组 $arr,ドル其中 $arr[i] = (2 \times i) + 1,ドル$(0 \le i < n)$。 + +在一次操作中,我们可以选出两个下标,记作 $x$ 和 $y$(0ドル \le x, y < n$),并使 $arr[x]$ 减去 1ドル,ドル$arr[y]$ 加上 1ドル$)。最终目标是使数组中所有元素都相等。 + +现在给定一个整数 $n,ドル即数组 $arr$ 的长度。 + +**要求**:返回使数组 $arr$ 中所有元素相等所需要的最小操作数。 + +**说明**: + +- 题目测试用例将会保证:在执行若干步操作后,数组中的所有元素最终可以全部相等。 +- 1ドル \le n \le 10^4$。 + +**示例**: + +- 示例 1: + +```Python +输入:n = 3 +输出:2 +解释:arr = [1, 3, 5] +第一次操作选出 x = 2 和 y = 0,使数组变为 [2, 3, 4] +第二次操作继续选出 x = 2 和 y = 0,数组将会变成 [3, 3, 3] +``` + +- 示例 2: + +```Python +输入:n = 6 +输出:9 +``` + +## 解题思路 + +### 思路 1:贪心 + +通过观察可以发现,数组中所有元素构成了一个等差数列,为了使所有元素相等,在每一次操作中,尽可能让较小值增大,让较大值减小,直到到达平均值为止,这样才能得到最小操作次数。 + +在一次操作中,我们可以同时让第 $i$ 个元素增大与第 $n - 1 - i$ 个元素减小。这样,我们只需要统计出数组前半部分元素变化幅度即可。 + +### 思路 1:代码 + +```Python +class Solution: + def minOperations(self, n: int) -> int: + ans = 0 + for i in range(n // 2): + ans += n - 1 - 2 * i + return ans +``` + +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n)$。 +- **空间复杂度**:$O(1)$。 + +### 思路 2:贪心 + 优化 + +数组前半部分元素变化幅度的计算可以看做是一个等差数列求和,所以我们可以直接根据高斯求和公式求出结果。 + +$\lbrace n - 1 + [n - 1 - 2 * (n \div 2 - 1)]\rbrace \times (n \div 2) \div 2 = n \times n \div 4$ + +### 思路 2:代码 + +```Python +class Solution: + def minOperations(self, n: int) -> int: + return n * n // 4 +``` + +### 思路 2:复杂度分析 + +- **时间复杂度**:$O(1)$。 +- **空间复杂度**:$O(1)$。 diff --git "a/Solutions/1716. 350円256円241円347円256円227円345円212円233円346円211円243円351円223円266円350円241円214円347円232円204円351円222円261円.md" "b/Solutions/1716. 350円256円241円347円256円227円345円212円233円346円211円243円351円223円266円350円241円214円347円232円204円351円222円261円.md" new file mode 100644 index 00000000..0f97e390 --- /dev/null +++ "b/Solutions/1716. 350円256円241円347円256円227円345円212円233円346円211円243円351円223円266円350円241円214円347円232円204円351円222円261円.md" @@ -0,0 +1,93 @@ +# [1716. 计算力扣银行的钱](https://leetcode.cn/problems/calculate-money-in-leetcode-bank/) + +- 标签:数学 +- 难度:简单 + +## 题目大意 + +**描述**:Hercy 每天都往力扣银行里存钱。 + +最开始,他在周一的时候存入 1ドル$ 块钱。从周二到周日,他每天都比前一天多存入 1ドル$ 块钱。在接下来的每个周一,他都会比前一个周一多存入 1ドル$ 块钱。 + +给定一个整数 $n$。 + +**要求**:计算在第 $n$ 天结束的时候,Hercy 在力扣银行中总共存了多少块钱。 + +**说明**: + +- 1ドル \le n \le 1000$。 + +**示例**: + +- 示例 1: + +```Python +输入:n = 4 +输出:10 +解释:第 4 天后,总额为 1 +たす 2 +たす 3 +たす 4 =わ 10。 +``` + +- 示例 2: + +```Python +输入:n = 10 +输出:37 +解释:第 10 天后,总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37 。注意到第二个星期一,Hercy 存入 2 块钱。 +``` + +## 解题思路 + +### 思路 1:暴力模拟 + +1. 记录当前周 $week$ 和当前周的当前天数 $day$。 +2. 按照题目要求,每天增加 1ドル$ 块钱,每周一比上周一增加 1ドル$ 块钱。这样,每天存钱数为 $week + day - 1$。 +3. 将每天存的钱数累加起来即为答案。 + +### 思路 1:代码 + +```Python +class Solution: + def totalMoney(self, n: int) -> int: + weak, day = 1, 1 + ans = 0 + for i in range(n): + ans += weak + day - 1 + day += 1 + if day == 8: + day = 1 + weak += 1 + + return ans +``` + +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n)$。 +- **空间复杂度**:$O(1)$。 + +### 思路 2:等差数列计算优化 + +每周一比上周一增加 1ドル$ 块钱,则每周七天存钱总数比上一周多 7ドル$ 块钱。所以每周存的钱数是一个等差数列。我们可以通过高斯求和公式求出所有整周存的钱数,再计算出剩下天数存的钱数,两者相加即为答案。 + +### 思路 2:代码 + +```python +class Solution: + def totalMoney(self, n: int) -> int: + week_cnt = n // 7 + weak_first_money = (1 + 7) * 7 // 2 + weak_last_money = weak_first_money + 7 * (week_cnt - 1) + week_ans = (weak_first_money + weak_last_money) * week_cnt // 2 + + day_cnt = n % 7 + day_first_money = 1 + week_cnt + day_last_money = day_first_money + day_cnt - 1 + day_ans = (day_first_money + day_last_money) * day_cnt // 2 + + return week_ans + day_ans +``` + +### 思路 2:复杂度分析 + +- **时间复杂度**:$O(1)$。 +- **空间复杂度**:$O(1)$。 diff --git a/Templates/01.Array/Array-BubbleSort.py b/Templates/01.Array/Array-BubbleSort.py index 350f3ea3..fe8131f2 100644 --- a/Templates/01.Array/Array-BubbleSort.py +++ b/Templates/01.Array/Array-BubbleSort.py @@ -1,7 +1,7 @@ class Solution: def bubbleSort(self, arr): # 第 i 趟排序 - for i in range(len(arr)): + for i in range(len(arr) - 1): # 从序列中前 n - i + 1 个元素的第 1 个元素开始,相邻两个元素进行比较 for j in range(len(arr) - i - 1): # 相邻两个元素进行比较,如果前者大于后者,则交换位置 diff --git a/Templates/02.LinkedList/LinkedList-BucketSort.py b/Templates/02.LinkedList/LinkedList-BucketSort.py index da015cb7..e9746cb3 100644 --- a/Templates/02.LinkedList/LinkedList-BucketSort.py +++ b/Templates/02.LinkedList/LinkedList-BucketSort.py @@ -70,7 +70,7 @@ def bucketSort(self, head: ListNode, bucket_size=5): # 计算桶的个数,并定义桶 bucket_count = (list_max - list_min) // bucket_size + 1 - buckets = [[] for _ in range(bucket_count)] + buckets = [None for _ in range(bucket_count)] # 将链表节点值依次添加到对应桶中 cur = head