diff --git a/.gitignore b/.gitignore index 27043361e..54f15dc05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ .DS_Store _book/ .idea/* -node_modules/ \ No newline at end of file +node_modules/ +book.pdf +book.mobi +book.epub +book.zip \ No newline at end of file diff --git a/README.md b/README.md index 6fc61c5be..1264bee1d 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,36 @@ ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluemaoj3j30z90dtmy5.jpg) -## 项目预览 +## 电子书 -![project-overview](https://github.com/azl397985856/leetcode/blob/master/Kapture%202020-08-19%20at%2011.37.36.gif) +这是我将我的所有公开的算法资料整理的一个电子书,全部题目信息中文化,以前会有一些英文描述。 -可以清晰地看出仓库组织关系。 +![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928846461-image.png) -(你可以下载本仓库 和 obsidian 软件,然后用 obsidian 打开获得更好的阅读效果) + +![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928862442-image.png) + +我写这本电子书花费了大量的时间和精力,除了内容上的创作,还要做一些电子书的排版,以让大家获得更好的阅读体验。光数学公式的展示,我就研究了多个插件的要源码,并魔改了一下才使得导出的电子书支持 latex。 不过有些动图,在做成电子书的时候自然就变没了,如果需要看动图的, 可以去我的公众号《力扣加加》或者我的 leetcode 题解仓库看。 + +由于是电子书,因此阅读体验可能比 Github 更好, 但是相应地就不能获得及时的更新,因此你可以收藏一下我的同步电子书的网站 [西法的刷题秘籍 - 在线版](https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/)。后期可能将每日一题, 91 天学算法其他章节的讲义等也整理进来。 + +电子书有更新我也会在公众号《力扣加加》进行通知, 感兴趣的同学可以关注一下。 + +目前导出了四种格式,可惜的是这几种格式都有自己的不足: + +- 在线版。 实时更新,想要及时获取最新信息的可以用在线版。 +- html。 方便大家在线观看,由于是 html ,实际上大家也可以保存起来**离线**观看。 +- pdf。可使用 pdf 阅读器和浏览器(比如谷歌)直接观看,阅读体验一般。 +- mobi。 下载一个 Kindle 客户端就可以看,不需要购买 Kindle。 +- epub。 数学公式和主题都比较不错, 但是代码没有高亮。 + +大家选择适合自己的格式下载即可。 + +- [在线版](https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/) +- [html](./assets/book.zip) +- [pdf](./book.pdf) +- [mobi](./book.mobi) +- [epub](./book.epub) ## 介绍 @@ -167,7 +190,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [《构造二叉树》专题](./selected/construct-binary-tree.md) - [字典序列删除](./selected/a-deleted.md) - [百度的算法面试题 - 祖玛游戏](./selected/zuma-game.md) -- [西法带你学算法】一次搞定前缀和](./selected/atMostK.md) +- [西法的刷题秘籍】一次搞定前缀和](./selected/atMostK.md) - [字节跳动的算法面试题是什么难度?](./selected/byte-dance-algo-ex.md) - [字节跳动的算法面试题是什么难度?(第二弹)](./selected/byte-dance-algo-ex-2017.md) - [《我是你的妈妈呀》 - 第一期](./selected/mother-01.md) diff --git a/SUMMARY.md b/SUMMARY.md index 1cda2630e..70e5f4373 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -2,229 +2,228 @@ # Summary​ -- [算法专题](thinkings/README.md) -- [数据结构](thinkings/basic-data-structure.md) -- [基础算法](thinkings/basic-algorithm.md) -- [二叉树的遍历](thinkings/binary-tree-traversal.md) -- [动态规划](thinkings/dynamic-programming.md) -- [哈夫曼编码和游程编码](thinkings/run-length-encode-and-huffman-encode.md) -- [布隆过滤器](thinkings/bloom-filter.md) -- [字符串问题](thinkings/string-problems.md) -- [前缀树专题](thinkings/trie.md) -- [《贪婪策略》专题](thinkings/greedy.md) -- [《深度优先遍历》专题](thinkings/DFS.md) -- [滑动窗口(思路 + 模板)](thinkings/slide-window.md) -- [位运算](thinkings/bit.md) -- [设计题](thinkings/design.md) -- [小岛问题](thinkings/island.md) -- [最大公约数](thinkings/GCD.md) -- [并查集](thinkings/union-find.md) -- [前缀和](thinkings/prefix.md) -- [平衡二叉树专题](thinkings/balanced-tree.md) +* [第一章 - 算法专题](thinkings/README.md) + * [数据结构](thinkings/basic-data-structure.md) + * [基础算法](thinkings/basic-algorithm.md) + * [二叉树的遍历](thinkings/binary-tree-traversal.md) + * [动态规划](thinkings/dynamic-programming.md) + * [哈夫曼编码和游程编码](thinkings/run-length-encode-and-huffman-encode.md) + * [布隆过滤器](thinkings/bloom-filter.md) + * [字符串问题](thinkings/string-problems.md) + * [前缀树专题](thinkings/trie.md) + * [《贪婪策略》专题](thinkings/greedy.md) + * [《深度优先遍历》专题](thinkings/DFS.md) + * [滑动窗口(思路 + 模板)](thinkings/slide-window.md) + * [位运算](thinkings/bit.md) + * [设计题](thinkings/design.md) + * [小岛问题](thinkings/island.md) + * [最大公约数](thinkings/GCD.md) + * [并查集](thinkings/union-find.md) + * [前缀和](thinkings/prefix.md) + * [平衡二叉树专题](thinkings/balanced-tree.md) -- [91 天学算法](./91/README.md) -- [第一期讲义-二分法](./91/binary-search.md) -- [第一期讲义-双指针](./91/two-pointers.md) -- [第二期](./91/season2.md) +* [第二章 - 91 天学算法](./91/README.md) + * [第一期讲义-二分法](./91/binary-search.md) + * [第一期讲义-双指针](./91/two-pointers.md) + * [第二期](./91/season2.md) -- [精选题解](./selected/README.md) -- [《日程安排》专题](./selected/schedule-topic.md) -- [《构造二叉树》专题](./selected/construct-binary-tree.md) -- [字典序列删除](./selected/a-deleted.md) -- [百度的算法面试题 - 祖玛游戏](./selected/zuma-game.md) -- [西法带你学算法】一次搞定前缀和](./selected/atMostK.md) -- [字节跳动的算法面试题是什么难度?](./selected/byte-dance-algo-ex.md) -- [字节跳动的算法面试题是什么难度?(第二弹)](./selected/byte-dance-algo-ex-2017.md) -- [《我是你的妈妈呀》 - 第一期](./selected/mother-01.md) -- [一文带你看懂二叉树的序列化](./selected/serialize.md) -- [穿上衣服我就不认识你了?来聊聊最长上升子序列](./selected/LIS.md) -- [你的衣服我扒了 - 《最长公共子序列》](./selected/LCS.md) -- [一文看懂《最大子序列和问题》](./selected/LSS.md) +* [第三章 - 精选题解](./selected/README.md) + * [《日程安排》专题](./selected/schedule-topic.md) + * [《构造二叉树》专题](./selected/construct-binary-tree.md) + * [字典序列删除](./selected/a-deleted.md) + * [百度的算法面试题 * 祖玛游戏](./selected/zuma-game.md) + * [西法的刷题秘籍】一次搞定前缀和](./selected/atMostK.md) + * [字节跳动的算法面试题是什么难度?](./selected/byte-dance-algo-ex.md) + * [字节跳动的算法面试题是什么难度?(第二弹)](./selected/byte-dance-algo-ex-2017.md) + * [《我是你的妈妈呀》 * 第一期](./selected/mother-01.md) + * [一文带你看懂二叉树的序列化](./selected/serialize.md) + * [穿上衣服我就不认识你了?来聊聊最长上升子序列](./selected/LIS.md) + * [你的衣服我扒了 * 《最长公共子序列》](./selected/LCS.md) + * [一文看懂《最大子序列和问题》](./selected/LSS.md) -- [高频考题(简单)](collections/easy.md) -- [面试题 17.12. BiNode](problems/binode-lcci.md) -- [0001. 两数之和](problems/1.two-sum.md) -- [0020. 有效的括号](problems/20.valid-parentheses.md) -- [0021. 合并两个有序链表](problems/21.merge-two-sorted-lists.md) -- [0026. 删除排序数组中的重复项](problems/26.remove-duplicates-from-sorted-array.md) -- [0053. 最大子序和](problems/53.maximum-sum-subarray-cn.md) -- [0088. 合并两个有序数组](problems/88.merge-sorted-array.md) -- [0101. 对称二叉树](problems/101.symmetric-tree.md) -- [0104. 二叉树的最大深度](problems/104.maximum-depth-of-binary-tree.md) -- [0108. 将有序数组转换为二叉搜索树](problems/108.convert-sorted-array-to-binary-search-tree.md) -- [0121. 买卖股票的最佳时机](problems/121.best-time-to-buy-and-sell-stock.md) -- [0122. 买卖股票的最佳时机 II](problems/122.best-time-to-buy-and-sell-stock-ii.md) -- [0125. 验证回文串](problems/125.valid-palindrome.md) -- [0136. 只出现一次的数字](problems/136.single-number.md) -- [0155. 最小栈](problems/155.min-stack.md) -- [0167. 两数之和 II - 输入有序数组](problems/167.two-sum-ii-input-array-is-sorted.md) -- [0169. 多数元素](problems/169.majority-element.md) -- [0172. 阶乘后的零](problems/172.factorial-trailing-zeroes.md) -- [0190. 颠倒二进制位](problems/190.reverse-bits.md) -- [0191. 位1的个数](problems/191.number-of-1-bits.md) -- [0198. 打家劫舍](problems/198.house-robber.md) -- [0203. 移除链表元素](problems/203.remove-linked-list-elements.md) -- [0206. 反转链表](problems/206.reverse-linked-list.md) -- [0219. 存在重复元素 II](problems/219.contains-duplicate-ii.md) -- [0226. 翻转二叉树](problems/226.invert-binary-tree.md) -- [0232. 用栈实现队列](problems/232.implement-queue-using-stacks.md) -- [0263. 丑数](problems/263.ugly-number.md) -- [0283. 移动零](problems/283.move-zeroes.md) -- [0342. 4的幂](problems/342.power-of-four.md) -- [0349. 两个数组的交集](problems/349.intersection-of-two-arrays.md) -- [0371. 两整数之和](problems/371.sum-of-two-integers.md) -- [0437. 路径总和 III](problems/437.path-sum-iii.md) -- [0455. 分发饼干](problems/455.AssignCookies.md) -- [0575. 分糖果](problems/575.distribute-candies.md) -- [0874. 模拟行走机器人](problems/874.walking-robot-simulation.md) -- [1260. 二维网格迁移](problems/1260.shift-2d-grid.md) -- [1332. 删除回文子序列](problems/1332.remove-palindromic-subsequences.md) +* [第四章 - 高频考题(简单)](collections/easy.md) + * [面试题 17.12. BiNode](problems/binode-lcci.md) + * [0001. 两数之和](problems/1.two-sum.md) + * [0020. 有效的括号](problems/20.valid-parentheses.md) + * [0021. 合并两个有序链表](problems/21.merge-two-sorted-lists.md) + * [0026. 删除排序数组中的重复项](problems/26.remove-duplicates-from-sorted-array.md) + * [0053. 最大子序和](problems/53.maximum-sum-subarray-cn.md) + * [0088. 合并两个有序数组](problems/88.merge-sorted-array.md) + * [0101. 对称二叉树](problems/101.symmetric-tree.md) + * [0104. 二叉树的最大深度](problems/104.maximum-depth-of-binary-tree.md) + * [0108. 将有序数组转换为二叉搜索树](problems/108.convert-sorted-array-to-binary-search-tree.md) + * [0121. 买卖股票的最佳时机](problems/121.best-time-to-buy-and-sell-stock.md) + * [0122. 买卖股票的最佳时机 II](problems/122.best-time-to-buy-and-sell-stock-ii.md) + * [0125. 验证回文串](problems/125.valid-palindrome.md) + * [0136. 只出现一次的数字](problems/136.single-number.md) + * [0155. 最小栈](problems/155.min-stack.md) + * [0167. 两数之和 II * 输入有序数组](problems/167.two-sum-ii-input-array-is-sorted.md) + * [0169. 多数元素](problems/169.majority-element.md) + * [0172. 阶乘后的零](problems/172.factorial-trailing-zeroes.md) + * [0190. 颠倒二进制位](problems/190.reverse-bits.md) + * [0191. 位1的个数](problems/191.number-of-1-bits.md) + * [0198. 打家劫舍](problems/198.house-robber.md) + * [0203. 移除链表元素](problems/203.remove-linked-list-elements.md) + * [0206. 反转链表](problems/206.reverse-linked-list.md) + * [0219. 存在重复元素 II](problems/219.contains-duplicate-ii.md) + * [0226. 翻转二叉树](problems/226.invert-binary-tree.md) + * [0232. 用栈实现队列](problems/232.implement-queue-using-stacks.md) + * [0263. 丑数](problems/263.ugly-number.md) + * [0283. 移动零](problems/283.move-zeroes.md) + * [0342. 4的幂](problems/342.power-of-four.md) + * [0349. 两个数组的交集](problems/349.intersection-of-two-arrays.md) + * [0371. 两整数之和](problems/371.sum-of-two-integers.md) + * [0437. 路径总和 III](problems/437.path-sum-iii.md) + * [0455. 分发饼干](problems/455.AssignCookies.md) + * [0575. 分糖果](problems/575.distribute-candies.md) + * [0874. 模拟行走机器人](problems/874.walking-robot-simulation.md) + * [1260. 二维网格迁移](problems/1260.shift-2d-grid.md) + * [1332. 删除回文子序列](problems/1332.remove-palindromic-subsequences.md) -- [高频考题(中等)](collections/medium.md) -- [面试题 17.09. 第 k 个数](problems/get-kth-magic-number-lcci.md) +* [第五章 - 高频考题(中等)](collections/medium.md) + * [0002. 两数相加](problems/2.add-two-numbers.md) + * [0003. 无重复字符的最长子串](problems/3.longest-substring-without-repeating-characters.md) + * [0005. 最长回文子串](problems/5.longest-palindromic-substring.md) + * [0011. 盛最多水的容器](problems/11.container-with-most-water.md) + * [0015. 三数之和](problems/15.3sum.md) + * [0017. 电话号码的字母组合](problems/17.Letter-Combinations-of-a-Phone-Number.md) + * [0019. 删除链表的倒数第N个节点](problems/19.removeNthNodeFromEndofList.md) + * [0022. 括号生成](problems/22.generate-parentheses.md) + * [0024. 两两交换链表中的节点](problems/24.swapNodesInPairs.md) + * [0029. 两数相除](problems/29.divide-two-integers.md) + * [0031. 下一个排列](problems/31.next-permutation.md) + * [0033. 搜索旋转排序数组](problems/33.search-in-rotated-sorted-array.md) + * [0039. 组合总和](problems/39.combination-sum.md) + * [0040. 组合总和 II](problems/40.combination-sum-ii.md) + * [0046. 全排列](problems/46.permutations.md) + * [0047. 全排列 II](problems/47.permutations-ii.md) + * [0048. 旋转图像](problems/48.rotate-image.md) + * [0049. 字母异位词分组](problems/49.group-anagrams.md) + * [0050. Pow(x, n)](problems/50.pow-x-n.md) + * [0055. 跳跃游戏](problems/55.jump-game.md) + * [0056. 合并区间](problems/56.merge-intervals.md) + * [0060. 第k个排列](problems/60.permutation-sequence.md) + * [0062. 不同路径](problems/62.unique-paths.md) + * [0073. 矩阵置零](problems/73.set-matrix-zeroes.md) + * [0075. 颜色分类](problems/75.sort-colors.md) + * [0078. 子集](problems/78.subsets.md) + * [0079. 单词搜索](problems/79.word-search.md) + * [0080. 删除排序数组中的重复项 II](problems/80.remove-duplicates-from-sorted-array-ii.md) + * [0086. 分隔链表](problems/86.partition-list.md) + * [0090. 子集 II](problems/90.subsets-ii.md) + * [0091. 解码方法](problems/91.decode-ways.md) + * [0092. 反转链表 II](problems/92.reverse-linked-list-ii.md) + * [0094. 二叉树的中序遍历](problems/94.binary-tree-inorder-traversal.md) + * [0095. 不同的二叉搜索树 II](problems/95.unique-binary-search-trees-ii.md) + * [0096. 不同的二叉搜索树](problems/96.unique-binary-search-trees.md) + * [0098. 验证二叉搜索树](problems/98.validate-binary-search-tree.md) + * [0102. 二叉树的层序遍历](problems/102.binary-tree-level-order-traversal.md) + * [0103. 二叉树的锯齿形层次遍历](problems/103.binary-tree-zigzag-level-order-traversal.md) + * [105. 从前序与中序遍历序列构造二叉树](problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md) + * [0113. 路径总和 II](problems/113.path-sum-ii.md) + * [0129. 求根到叶子节点数字之和](problems/129.sum-root-to-leaf-numbers.md) + * [0130. 被围绕的区域](problems/130.surrounded-regions.md) + * [0131. 分割回文串](problems/131.palindrome-partitioning.md) + * [0139. 单词拆分](problems/139.word-break.md) + * [0144. 二叉树的前序遍历](problems/144.binary-tree-preorder-traversal.md) + * [0150. 逆波兰表达式求值](problems/150.evaluate-reverse-polish-notation.md) + * [0152. 乘积最大子数组](problems/152.maximum-product-subarray.md) + * [0199. 二叉树的右视图](problems/199.binary-tree-right-side-view.md) + * [0200. 岛屿数量](problems/200.number-of-islands.md) + * [0201. 数字范围按位与](problems/201.bitwise-and-of-numbers-range.md) + * [0208. 实现 Trie (前缀树)](problems/208.implement-trie-prefix-tree.md) + * [0209. 长度最小的子数组](problems/209.minimum-size-subarray-sum.md) + * [0211. 添加与搜索单词 * 数据结构设计](problems/211.add-and-search-word-data-structure-design.md) + * [0215. 数组中的第K个最大元素](problems/215.kth-largest-element-in-an-array.md) + * [0221. 最大正方形](problems/221.maximal-square.md) + * [0229. 求众数 II](problems/229.majority-element-ii.md) + * [0230. 二叉搜索树中第K小的元素](problems/230.kth-smallest-element-in-a-bst.md) + * [0236. 二叉树的最近公共祖先](problems/236.lowest-common-ancestor-of-a-binary-tree.md) + * [0238. 除自身以外数组的乘积](problems/238.product-of-array-except-self.md) + * [0240. 搜索二维矩阵 II](problems/240.search-a-2-d-matrix-ii.md) + * [0279. 完全平方数](problems/279.perfect-squares.md) + * [0309. 最佳买卖股票时机含冷冻期](problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) + * [0322. 零钱兑换](problems/322.coin-change.md) + * [0328. 奇偶链表](problems/328.odd-even-linked-list.md) + * [0334. 递增的三元子序列](problems/334.increasing-triplet-subsequence.md) + * [0337. 打家劫舍 III](problems/337.house-robber-iii.md) + * [0343. 整数拆分](problems/343.integer-break.md) + * [0365. 水壶问题](problems/365.water-and-jug-problem.md) + * [0378. 有序矩阵中第K小的元素](problems/378.kth-smallest-element-in-a-sorted-matrix.md) + * [0380. 常数时间插入、删除和获取随机元素](problems/380.insert-delete-getrandom-o1.md) + * [0416. 分割等和子集](problems/416.partition-equal-subset-sum.md) + * [0445. 两数相加 II](problems/445.add-two-numbers-ii.md) + * [0454. 四数相加 II](problems/454.4-sum-ii.md) + * [0494. 目标和](problems/494.target-sum.md) + * [0516. 最长回文子序列](problems/516.longest-palindromic-subsequence.md) + * [0518. 零钱兑换 II](problems/518.coin-change-2.md) + * [0547. 朋友圈](problems/547.friend-circles.md) + * [0560. 和为K的子数组](problems/560.subarray-sum-equals-k.md) + * [0609. 在系统中查找重复文件](problems/609.find-duplicate-file-in-system.md) + * [0611. 有效三角形的个数](problems/611.valid-triangle-number.md) + * [0718. 最长重复子数组](problems/718.maximum-length-of-repeated-subarray.md) + * [0754. 到达终点数字](problems/754.reach-a-number.md) + * [0785. 判断二分图](problems/785.is-graph-bipartite.md) + * [0820. 单词的压缩编码](problems/820.short-encoding-of-words.md) + * [0875. 爱吃香蕉的珂珂](problems/875.koko-eating-bananas.md) + * [0877. 石子游戏](problems/877.stone-game.md) + * [0886. 可能的二分法](problems/886.possible-bipartition.md) + * [0900. RLE 迭代器](problems/900.rle-iterator.md) + * [0912. 排序数组](problems/912.sort-an-array.md) + * [0935. 骑士拨号器](problems/935.knight-dialer.md) + * [1011. 在 D 天内送达包裹的能力](problems/1011.capacity-to-ship-packages-within-d-days.md) + * [1014. 最佳观光组合](problems/1014.best-sightseeing-pair.md) + * [1015. 可被 K 整除的最小整数](problems/1015.smallest-integer-divisible-by-k.md) + * [1019. 链表中的下一个更大节点](problems/1019.next-greater-node-in-linked-list.md) + * [1020. 飞地的数量](problems/1020.number-of-enclaves.md) + * [1023. 驼峰式匹配](problems/1023.camelcase-matching.md) + * [1031. 两个非重叠子数组的最大和](problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md) + * [1104. 二叉树寻路](problems/1104.path-in-zigzag-labelled-binary-tree.md) + * [1131.绝对值表达式的最大值](problems/1131.maximum-of-absolute-value-expression.md) + * [1186. 删除一次得到子数组最大和](problems/1186.maximum-subarray-sum-with-one-deletion.md) + * [1218. 最长定差子序列](problems/1218.longest-arithmetic-subsequence-of-given-difference.md) + * [1227. 飞机座位分配概率](problems/1227.airplane-seat-assignment-probability.md) + * [1261. 在受污染的二叉树中查找元素](problems/1261.find-elements-in-a-contaminated-binary-tree.md) + * [1262. 可被三整除的最大和](problems/1262.greatest-sum-divisible-by-three.md) + * [1297. 子串的最大出现次数](problems/1297.maximum-number-of-occurrences-of-a-substring.md) + * [1310. 子数组异或查询](problems/1310.xor-queries-of-a-subarray.md) + * [1334. 阈值距离内邻居最少的城市](problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md) + * [1371.每个元音包含偶数次的最长子字符串](problems/1371.find-the-longest-substring-containing-vowels-in-even-counts.md) -- [0002. 两数相加](problems/2.add-two-numbers.md) -- [0003. 无重复字符的最长子串](problems/3.longest-substring-without-repeating-characters.md) -- [0005. 最长回文子串](problems/5.longest-palindromic-substring.md) -- [0011. 盛最多水的容器](problems/11.container-with-most-water.md) -- [0015. 三数之和](problems/15.3sum.md) -- [0017. 电话号码的字母组合](problems/17.Letter-Combinations-of-a-Phone-Number.md) -- [0019. 删除链表的倒数第N个节点](problems/19.removeNthNodeFromEndofList.md) -- [0022. 括号生成](problems/22.generate-parentheses.md) -- [0024. 两两交换链表中的节点](problems/24.swapNodesInPairs.md) -- [0029. 两数相除](problems/29.divide-two-integers.md) -- [0031. 下一个排列](problems/31.next-permutation.md) -- [0033. 搜索旋转排序数组](problems/33.search-in-rotated-sorted-array.md) -- [0039. 组合总和](problems/39.combination-sum.md) -- [0040. 组合总和 II](problems/40.combination-sum-ii.md) -- [0046. 全排列](problems/46.permutations.md) -- [0047. 全排列 II](problems/47.permutations-ii.md) -- [0048. 旋转图像](problems/48.rotate-image.md) -- [0049. 字母异位词分组](problems/49.group-anagrams.md) -- [0050. Pow(x, n)](problems/50.pow-x-n.md) -- [0055. 跳跃游戏](problems/55.jump-game.md) -- [0056. 合并区间](problems/56.merge-intervals.md) -- [0060. 第k个排列](problems/60.permutation-sequence.md) -- [0062. 不同路径](problems/62.unique-paths.md) -- [0073. 矩阵置零](problems/73.set-matrix-zeroes.md) -- [0075. 颜色分类](problems/75.sort-colors.md) -- [0078. 子集](problems/78.subsets.md) -- [0079. 单词搜索](problems/79.word-search.md) -- [0080. 删除排序数组中的重复项 II](problems/80.remove-duplicates-from-sorted-array-ii.md) -- [0086. 分隔链表](problems/86.partition-list.md) -- [0090. 子集 II](problems/90.subsets-ii.md) -- [0091. 解码方法](problems/91.decode-ways.md) -- [0092. 反转链表 II](problems/92.reverse-linked-list-ii.md) -- [0094. 二叉树的中序遍历](problems/94.binary-tree-inorder-traversal.md) -- [0095. 不同的二叉搜索树 II](problems/95.unique-binary-search-trees-ii.md) -- [0096. 不同的二叉搜索树](problems/96.unique-binary-search-trees.md) -- [0098. 验证二叉搜索树](problems/98.validate-binary-search-tree.md) -- [0102. 二叉树的层序遍历](problems/102.binary-tree-level-order-traversal.md) -- [0103. 二叉树的锯齿形层次遍历](problems/103.binary-tree-zigzag-level-order-traversal.md) -- [105. 从前序与中序遍历序列构造二叉树](problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md) -- [0113. 路径总和 II](problems/113.path-sum-ii.md) -- [0129. 求根到叶子节点数字之和](problems/129.sum-root-to-leaf-numbers.md) -- [0130. 被围绕的区域](problems/130.surrounded-regions.md) -- [0131. 分割回文串](problems/131.palindrome-partitioning.md) -- [0139. 单词拆分](problems/139.word-break.md) -- [0144. 二叉树的前序遍历](problems/144.binary-tree-preorder-traversal.md) -- [0150. 逆波兰表达式求值](problems/150.evaluate-reverse-polish-notation.md) -- [0152. 乘积最大子数组](problems/152.maximum-product-subarray.md) -- [0199. 二叉树的右视图](problems/199.binary-tree-right-side-view.md) -- [0200. 岛屿数量](problems/200.number-of-islands.md) -- [0201. 数字范围按位与](problems/201.bitwise-and-of-numbers-range.md) -- [0208. 实现 Trie (前缀树)](problems/208.implement-trie-prefix-tree.md) -- [0209. 长度最小的子数组](problems/209.minimum-size-subarray-sum.md) -- [0211. 添加与搜索单词 - 数据结构设计](problems/211.add-and-search-word-data-structure-design.md) -- [0215. 数组中的第K个最大元素](problems/215.kth-largest-element-in-an-array.md) -- [0221. 最大正方形](problems/221.maximal-square.md) -- [0229. 求众数 II](problems/229.majority-element-ii.md) -- [0230. 二叉搜索树中第K小的元素](problems/230.kth-smallest-element-in-a-bst.md) -- [0236. 二叉树的最近公共祖先](problems/236.lowest-common-ancestor-of-a-binary-tree.md) -- [0238. 除自身以外数组的乘积](problems/238.product-of-array-except-self.md) -- [0240. 搜索二维矩阵 II](problems/240.search-a-2-d-matrix-ii.md) -- [0279. 完全平方数](problems/279.perfect-squares.md) -- [0309. 最佳买卖股票时机含冷冻期](problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) -- [0322. 零钱兑换](problems/322.coin-change.md) -- [0328. 奇偶链表](problems/328.odd-even-linked-list.md) -- [0334. 递增的三元子序列](problems/334.increasing-triplet-subsequence.md) -- [0337. 打家劫舍 III](problems/337.house-robber-iii.md) -- [0343. 整数拆分](problems/343.integer-break.md) -- [0365. 水壶问题](problems/365.water-and-jug-problem.md) -- [0378. 有序矩阵中第K小的元素](problems/378.kth-smallest-element-in-a-sorted-matrix.md) -- [0380. 常数时间插入、删除和获取随机元素](problems/380.insert-delete-getrandom-o1.md) -- [0416. 分割等和子集](problems/416.partition-equal-subset-sum.md) -- [0445. 两数相加 II](problems/445.add-two-numbers-ii.md) -- [0454. 四数相加 II](problems/454.4-sum-ii.md) -- [0494. 目标和](problems/494.target-sum.md) -- [0516. 最长回文子序列](problems/516.longest-palindromic-subsequence.md) -- [0518. 零钱兑换 II](problems/518.coin-change-2.md) -- [0547. 朋友圈](problems/547.friend-circles.md) -- [0560. 和为K的子数组](problems/560.subarray-sum-equals-k.md) -- [0609. 在系统中查找重复文件](problems/609.find-duplicate-file-in-system.md) -- [0611. 有效三角形的个数](problems/611.valid-triangle-number.md) -- [0718. 最长重复子数组](problems/718.maximum-length-of-repeated-subarray.md) -- [0754. 到达终点数字](problems/754.reach-a-number.md) -- [0785. 判断二分图](problems/785.is-graph-bipartite.md) -- [0820. 单词的压缩编码](problems/820.short-encoding-of-words.md) -- [0875. 爱吃香蕉的珂珂](problems/875.koko-eating-bananas.md) -- [0877. 石子游戏](problems/877.stone-game.md) -- [0886. 可能的二分法](problems/886.possible-bipartition.md) -- [0900. RLE 迭代器](problems/900.rle-iterator.md) -- [0912. 排序数组](problems/912.sort-an-array.md) -- [0935. 骑士拨号器](problems/935.knight-dialer.md) -- [1011. 在 D 天内送达包裹的能力](problems/1011.capacity-to-ship-packages-within-d-days.md) -- [1014. 最佳观光组合](problems/1014.best-sightseeing-pair.md) -- [1015. 可被 K 整除的最小整数](problems/1015.smallest-integer-divisible-by-k.md) -- [1019. 链表中的下一个更大节点](problems/1019.next-greater-node-in-linked-list.md) -- [1020. 飞地的数量](problems/1020.number-of-enclaves.md) -- [1023. 驼峰式匹配](problems/1023.camelcase-matching.md) -- [1031. 两个非重叠子数组的最大和](problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md) -- [1104. 二叉树寻路](problems/1104.path-in-zigzag-labelled-binary-tree.md) -- [1131.绝对值表达式的最大值](problems/1131.maximum-of-absolute-value-expression.md) -- [1186. 删除一次得到子数组最大和](problems/1186.maximum-subarray-sum-with-one-deletion.md) -- [1218. 最长定差子序列](problems/1218.longest-arithmetic-subsequence-of-given-difference.md) -- [1227. 飞机座位分配概率](problems/1227.airplane-seat-assignment-probability.md) -- [1261. 在受污染的二叉树中查找元素](problems/1261.find-elements-in-a-contaminated-binary-tree.md) -- [1262. 可被三整除的最大和](problems/1262.greatest-sum-divisible-by-three.md) -- [1297. 子串的最大出现次数](problems/1297.maximum-number-of-occurrences-of-a-substring.md) -- [1310. 子数组异或查询](problems/1310.xor-queries-of-a-subarray.md) -- [1334. 阈值距离内邻居最少的城市](problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md) -- [1371.每个元音包含偶数次的最长子字符串](problems/1371.find-the-longest-substring-containing-vowels-in-even-counts.md) -- [高频考题(困难)](collections/hard.md) +* [第六章 - 高频考题(困难)](collections/hard.md) + * [0004. 寻找两个正序数组的中位数](problems/4.median-of-two-sorted-arrays.md) + * [0023. 合并K个升序链表](problems/23.merge-k-sorted-lists.md) + * [0025. K 个一组翻转链表](problems/25.reverse-nodes-in-k-groups.md) + * [0030. 串联所有单词的子串](problems/30.substring-with-concatenation-of-all-words.md) + * [0032. 最长有效括号](problems/32.longest-valid-parentheses.md) + * [0042. 接雨水](problems/42.trapping-rain-water.md) + * [0052. N皇后 II](problems/52.N-Queens-II.md) + * [0084. 柱状图中最大的矩形](problems/84.largest-rectangle-in-histogram.md) + * [0085. 最大矩形](problems/85.maximal-rectangle.md) + * [0124. 二叉树中的最大路径和](problems/124.binary-tree-maximum-path-sum.md) + * [0128. 最长连续序列](problems/128.longest-consecutive-sequence.md) + * [0145. 二叉树的后序遍历](problems/145.binary-tree-postorder-traversal.md) + * [0212. 单词搜索 II](problems/212.word-search-ii.md) + * [0239. 滑动窗口最大值](problems/239.sliding-window-maximum.md) + * [0295. 数据流的中位数](problems/295.find-median-from-data-stream.md) + * [0301. 删除无效的括号](problems/301.remove-invalid-parentheses.md) + * [0312. 戳气球](problems/312.burst-balloons.md) + * [0335. 路径交叉](problems/335.self-crossing.md) + * [0460. LFU缓存](problems/460.lfu-cache.md) + * [0472. 连接词](problems/472.concatenated-words.md) + * [0488. 祖玛游戏](problems/488.zuma-game.md) + * [0493. 翻转对](problems/493.reverse-pairs.md) + * [0887. 鸡蛋掉落](problems/887.super-egg-drop.md) + * [0895. 最大频率栈](problems/895.maximum-frequency-stack.md) + * [1032. 字符流](problems/1032.stream-of-characters.md) + * [1168. 水资源分配优化](problems/1168.optimize-water-distribution-in-a-village.md) + * [1449. 数位成本和为目标值的最大数字](problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md) + -- [0004. 寻找两个正序数组的中位数](problems/4.median-of-two-sorted-arrays.md) -- [0023. 合并K个升序链表](problems/23.merge-k-sorted-lists.md) -- [0025. K 个一组翻转链表](problems/25.reverse-nodes-in-k-groups.md) -- [0030. 串联所有单词的子串](problems/30.substring-with-concatenation-of-all-words.md) -- [0032. 最长有效括号](problems/32.longest-valid-parentheses.md) -- [0042. 接雨水](problems/42.trapping-rain-water.md) -- [0052. N皇后 II](problems/52.N-Queens-II.md) -- [0084. 柱状图中最大的矩形](problems/84.largest-rectangle-in-histogram.md) -- [0085. 最大矩形](problems/85.maximal-rectangle.md) -- [0124. 二叉树中的最大路径和](problems/124.binary-tree-maximum-path-sum.md) -- [0128. 最长连续序列](problems/128.longest-consecutive-sequence.md) -- [0145. 二叉树的后序遍历](problems/145.binary-tree-postorder-traversal.md) -- [0212. 单词搜索 II](problems/212.word-search-ii.md) -- [0239. 滑动窗口最大值](problems/239.sliding-window-maximum.md) -- [0295. 数据流的中位数](problems/295.find-median-from-data-stream.md) -- [0301. 删除无效的括号](problems/301.remove-invalid-parentheses.md) -- [0312. 戳气球](problems/312.burst-balloons.md) -- [0335. 路径交叉](problems/335.self-crossing.md) -- [0460. LFU缓存](problems/460.lfu-cache.md) -- [0472. 连接词](problems/472.concatenated-words.md) -- [0488. 祖玛游戏](problems/488.zuma-game.md) -- [0493. 翻转对](problems/493.reverse-pairs.md) -- [0887. 鸡蛋掉落](problems/887.super-egg-drop.md) -- [0895. 最大频率栈](problems/895.maximum-frequency-stack.md) -- [1032. 字符流](problems/1032.stream-of-characters.md) -- [1168. 水资源分配优化](problems/1168.optimize-water-distribution-in-a-village.md) -- [1449. 数位成本和为目标值的最大数字](problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md) - -- [后序](./epilogue.md) +* [后序](./epilogue.md) diff --git a/book.epub b/book.epub deleted file mode 100644 index bb84fe123..000000000 Binary files a/book.epub and /dev/null differ diff --git a/book.json b/book.json index 5473366f3..6877d0bf5 100644 --- a/book.json +++ b/book.json @@ -2,6 +2,11 @@ "structure": { "readme": "introduction.md" }, - "language": "zh-hans" - + "language": "zh-hans", + "plugins": ["katex", "ace", "theme-api"], + "pluginsConfig": { + "theme-api": { + "theme": "dark" + } + } } diff --git a/book.mobi b/book.mobi deleted file mode 100644 index a485ea059..000000000 Binary files a/book.mobi and /dev/null differ diff --git a/book.pdf b/book.pdf deleted file mode 100644 index 9f42a6c21..000000000 Binary files a/book.pdf and /dev/null differ diff --git a/collections/easy.md b/collections/easy.md index 754e6fb18..8665a3d8a 100644 --- a/collections/easy.md +++ b/collections/easy.md @@ -1,3 +1,9 @@ +# 简单难度题目合集 + +这里的题目难度比较小, 大多是模拟题,或者是很容易看出解法的题目,另外简单题目一般使用暴力法都是可以解决的。 这个时候只有看一下数据范围,思考下你的算法复杂度就行了。 + +当然也不排除很多 hard 题目也可以暴力模拟,大家平时多注意数据范围即可。 + - [面试题 17.12. BiNode](../problems/binode-lcci.md) 🆕 - [0001.two-sum](https://github.com/azl397985856/leetcode/blob/master/problems/1.two-sum.md) diff --git a/collections/hard.md b/collections/hard.md index 79ee4b2d3..cd4c17ced 100644 --- a/collections/hard.md +++ b/collections/hard.md @@ -1,3 +1,27 @@ +# 困难难度题目合集 + +困难难度题目从类型上说多是: + +- 图 +- 设计题 +- 游戏场景题目 +- 中等题目的 follow up + +从解法上来说,多是: + +- 图算法 +- 动态规划 +- 二分法 +- DFS & BFS +- 状态压缩 +- 剪枝 + +从逻辑上说, 要么就是非常难想到,要么就是非常难写代码。 这里我总结了几个技巧: + +1. 看题目的数据范围, 看能否暴力模拟 +2. 暴力枚举所有可能的算法往上套 +3. 总结和记忆解题模板,减少解题压力 + - [0004.median-of-two-sorted-array](../problems/4.median-of-two-sorted-arrays.md) - [0023.merge-k-sorted-lists](../problems/23.merge-k-sorted-lists.md) - [0025.reverse-nodes-in-k-group](../problems/25.reverse-nodes-in-k-groups-cn.md) diff --git a/collections/medium.md b/collections/medium.md index dd93722f8..9a52d8154 100644 --- a/collections/medium.md +++ b/collections/medium.md @@ -1,3 +1,9 @@ +# 中等难度题目合集 + +中等题目是力扣比例最大的部分,因此这部分我的题解也是最多的。 大家不要太过追求难题,先把中等难度题目做熟了再说。 + +这部分的题目要不需要我们挖掘题目的内含信息, 将其抽象成简单题目。 要么是一些写起来比较麻烦的题目, 一些人编码能力不行就挂了。因此大家一定要自己做, 即使看了题解”会了“,也要自己码一遍。自己不亲自写一遍,里面的细节永远不知道。 + - [面试题 17.09. 第 k 个数](../problems/get-kth-magic-number-lcci.md) - [0002.add-two-numbers](../problems/2.add-two-numbers.md) @@ -26,7 +32,7 @@ - [0073.set-matrix-zeroes](../problems/73.set-matrix-zeroes.md) - [0075.sort-colors](../problems/75.sort-colors.md) - [0078.subsets](../problems/78.subsets.md) -- [0079.word-search](../problems/79.word-search-en.md) +- [0079.word-search](../problems/79.word-search.md) - [0080.remove-duplicates-from-sorted-array-ii](../problems/80.remove-duplicates-from-sorted-array-ii.md) 🆕 - [0086.partition-list](../problems/86.partition-list.md) - [0090.subsets-ii](../problems/90.subsets-ii.md) diff --git a/cover.jpg b/cover.jpg new file mode 100644 index 000000000..6bc6d1426 Binary files /dev/null and b/cover.jpg differ diff --git a/epilogue.md b/epilogue.md new file mode 100644 index 000000000..8fcb275b7 --- /dev/null +++ b/epilogue.md @@ -0,0 +1,13 @@ +# 后记 + +以上就是本电子书的全部内容了。如果你觉得这本书对你有用, 那么请将它分享给你身边的朋友,你的点赞和分享是我最大的动力。另外由于本人水平和精力有限,难免有不正确的地方,大家可以通过 github 的 pr 给我指正,感谢大家。 + +后期的话文章会第一时间在公众号和我的博客更新,并定期整理到这个电子书中来。因此你可以关注我的公众号或者博客, 也可以关注我的同步电子书的网站 [西法的刷题秘籍 - 在线版](https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/) 获得内容的更新。 + +如果想加入读者交流群, 在公众号回复 leetcode 即可,西法在群里等着你。 + +关注公众号《力扣加加》带你啃下算法这块硬骨头。 + +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + +lucifer 的博客地址:https://lucifer.ren/blog/ diff --git a/introduction.md b/introduction.md index faa5df477..c6d4260fd 100644 --- a/introduction.md +++ b/introduction.md @@ -11,17 +11,40 @@ - 2019-10-08: [纪念 LeetCode 项目 Star 突破 2W](./thanksGiving2.md),并且 Github 搜索“LeetCode”,排名第一。 - 2020-04-12: [项目突破三万 Star](./thanksGiving3.md)。 + - 2020-04-14: 官网`力扣加加`上线啦 💐💐💐💐💐,有专题讲解,每日一题,下载区和视频题解,后续会增加更多内容,还不赶紧收藏起来?地址:http://leetcode-solution.cn/ ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluemaoj3j30z90dtmy5.jpg) ## 前言 -这是我将我的所有公开的算法资料整理的一个电子书,主要特点有: +这是我将我的所有公开的算法资料整理的一个电子书,全部题目信息中文化,以前会有一些英文描述,感谢 @CYL 的中文整理。 + +![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928846461-image.png) + + +![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928862442-image.png) + +我写这本电子书花费了大量的时间和精力,除了内容上的创作,还要做一些电子书的排版,以让大家获得更好的阅读体验。光数学公式的展示,我就研究了多个插件的要源码,并魔改了一下才使得导出的电子书支持 latex。 不过有些动图,在做成电子书的时候自然就变没了,如果需要看动图的, 可以去我的公众号《力扣加加》或者我的 leetcode 题解仓库看。 + +由于是电子书,因此阅读体验可能会更好, 但是相应地就不能获得及时的更新,因此你可以收藏一下我的同步电子书的网站 [西法的刷题秘籍 - 在线版](https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/)。后期可能将每日一题, 91 天学算法其他章节的讲义等也整理进来。 + +电子书有更新我也会在公众号《力扣加加》进行通知, 感兴趣的同学可以关注一下。 + +目前导出了四种格式,可惜的是这几种格式都有自己的不足: + +- 在线版。 实时更新,想要及时获取最新信息的可以用在线版。 +- html。 方便大家在线观看,由于是 html ,实际上大家也可以保存起来**离线**观看。 +- pdf。可使用 pdf 阅读器和浏览器(比如谷歌)直接观看,阅读体验一般,生成的目录不能导航。 +- mobi。 下载一个 Kindle 客户端就可以看,不需要购买 Kindle。 +- epub。 数学公式和主题都比较不错, 但是代码没有高亮。 + +大家选择适合自己的格式下载即可。 + +- [在线版](https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/) -- 全部题目信息中文化,以前会有一些英文描述。 -后期可能将每日一题也整理进来。 +html, pdf,mobi 和 epub 格式,关注我的公众号《力扣加加》回复`电子书`即可。 ## 介绍 @@ -63,6 +86,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。 ## 食用指南 +- 我对大部分题目的复杂度都进行了分析,除了个别分析起来复杂的题目,大家一定要对一道题的复杂度了如指掌才可以。 +> 有些题目我是故意不写的, 比如所有的回溯题目我都没写, 不过它们全部都是指数的复杂度 - 我对题目难度进行了分类的保留,因此你可以根据自己的情况刷。我推荐大家从简单开始,逐步加大难度,直到困难。 - 这里有一张互联网公司面试中经常考察的问题类型总结的思维导图,我们可以结合图片中的信息分析一下。 diff --git a/package.json b/package.json new file mode 100644 index 000000000..1fb3ab648 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "mathjax": "^2.7.6" + } +} diff --git a/problems/1.two-sum.md b/problems/1.two-sum.md index 7a04aad22..780dc48a1 100644 --- a/problems/1.two-sum.md +++ b/problems/1.two-sum.md @@ -76,7 +76,7 @@ const twoSum = function (nums, target) { - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/101.symmetric-tree.md b/problems/101.symmetric-tree.md index 2e58602c9..78fcda998 100644 --- a/problems/101.symmetric-tree.md +++ b/problems/101.symmetric-tree.md @@ -119,8 +119,8 @@ class Solution: - 时间复杂度:$O(N)$,其中 N 为节点数。 - 空间复杂度:递归的深度最高为节点数,因此空间复杂度是 $O(N)$,其中 N 为节点数。 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 - -大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9b4p9ej30x20iwjtf.jpg) diff --git a/problems/1011.capacity-to-ship-packages-within-d-days.md b/problems/1011.capacity-to-ship-packages-within-d-days.md index cc9935cd4..eca3366bb 100644 --- a/problems/1011.capacity-to-ship-packages-within-d-days.md +++ b/problems/1011.capacity-to-ship-packages-within-d-days.md @@ -174,6 +174,7 @@ var shipWithinDays = function(weights, D) { }; ``` -## 扩展 +**复杂度分析** -## 参考 +- 时间复杂度:$O(logN)$ +- 空间复杂度:$O(N)$ diff --git a/problems/1014.best-sightseeing-pair.md b/problems/1014.best-sightseeing-pair.md index c5af4ea8c..d2ad2fbf9 100644 --- a/problems/1014.best-sightseeing-pair.md +++ b/problems/1014.best-sightseeing-pair.md @@ -103,3 +103,12 @@ class Solution: pre = pre if pre > A[i] + i else A[i] + i return res ``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1015.smallest-integer-divisible-by-k.md b/problems/1015.smallest-integer-divisible-by-k.md index 1f1ffa4ad..80c78e71a 100644 --- a/problems/1015.smallest-integer-divisible-by-k.md +++ b/problems/1015.smallest-integer-divisible-by-k.md @@ -97,10 +97,6 @@ class Solution: if mod == 0: return ix seen.add(mod) - - # @lc code=end - - ``` ## 相关题目 diff --git a/problems/1019.next-greater-node-in-linked-list.md b/problems/1019.next-greater-node-in-linked-list.md index 2b8400952..a39901e92 100644 --- a/problems/1019.next-greater-node-in-linked-list.md +++ b/problems/1019.next-greater-node-in-linked-list.md @@ -103,6 +103,13 @@ class Solution: return res ``` +**复杂度分析** + +其中 N 为链表的长度。 + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 扩展 甚至可以做到 O(1)的空间复杂度,请参考[C# O(n) time O(1) space]() diff --git a/problems/1020.number-of-enclaves.md b/problems/1020.number-of-enclaves.md index b39b75759..f7235f6c5 100644 --- a/problems/1020.number-of-enclaves.md +++ b/problems/1020.number-of-enclaves.md @@ -11,7 +11,6 @@ https://leetcode-cn.com/problems/number-of-enclaves/ 返回网格中无法在任意次数的移动中离开网格边界的陆地单元格的数量。 -  示例 1: @@ -53,14 +52,14 @@ https://leetcode-cn.com/problems/number-of-enclaves/ - 如果遍历到 0,我们不予理会 - 如果遍历到 1. 我们将其加到 temp -- 我们不断拓展边界(上下左右) -- 如果 dfs 过程中碰到了边界,说明我们可以逃脱,我们将累加的 temp 清空 -- 如果 dfs 过程之后没有碰到边界,说明我们无法逃脱。我们将 temp 加到 cnt +- 不断拓展边界(上下左右) +- 如果 dfs 过程中碰到了边界,说明可以逃脱,我们将累加的 temp 清空 +- 如果 dfs 过程之后没有碰到边界,说明无法逃脱。我们将 temp 加到 cnt - 最终返回 cnt 即可 ### 关键点解析 -- visited 记录访问过的节点,防止重复计算 +- visited 记录访问过的节点,防止无限循环。 ### 代码 @@ -102,6 +101,11 @@ class Solution: ``` +**复杂度分析** + +- 时间复杂度:$O(M * N)$ +- 空间复杂度:$O(M * N)$ + ## 解法二 (消除法) ## 公司 @@ -110,7 +114,7 @@ class Solution: ### 思路 -上面的解法时间复杂度和空间复杂度都很差,我们考虑进行优化, 这里我们使用消除法。 +上面的解法空间复杂度很差,我们考虑进行优化, 这里我们使用消除法。即使用题目范围外的数据原地标记是否访问, 这样时间复杂度可以优化到 $O(1)$,这是一种非常常见的优化技巧,请务必掌握,另外文章末尾的题目也是类似的技巧,大家可以结合起来练习。 - 从矩阵边界开始 dfs - 如果碰到 1 就将其变成 0 @@ -167,6 +171,11 @@ class Solution: ``` +**复杂度分析** + +- 时间复杂度:$O(M * N)$ +- 空间复杂度:$O(1)$ + ## 参考 - [200.number-of-islands](https://github.com/azl397985856/leetcode/blob/master/problems/200.number-of-islands.md) diff --git a/problems/1023.camelcase-matching.md b/problems/1023.camelcase-matching.md index 3a576f247..2c0c8ac3b 100644 --- a/problems/1023.camelcase-matching.md +++ b/problems/1023.camelcase-matching.md @@ -135,6 +135,13 @@ class Solution: return res ``` +**复杂度分析** + +其中 N 为 queries 的长度, M 为 queries 的平均长度, P 为 pattern 的长度。 + +- 时间复杂度:$O(N * M * P)$ +- 空间复杂度:$O(1)$ + ## 扩展 这是一个符合直觉的解法,但是却不是一个很优秀的解法,那么你有想到什么优秀的解法么? diff --git a/problems/103.binary-tree-zigzag-level-order-traversal.md b/problems/103.binary-tree-zigzag-level-order-traversal.md index a37159a59..892b76e20 100644 --- a/problems/103.binary-tree-zigzag-level-order-traversal.md +++ b/problems/103.binary-tree-zigzag-level-order-traversal.md @@ -63,52 +63,6 @@ https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ JavaScript Code: ```js -/* - * @lc app=leetcode id=103 lang=javascript - * - * [103] Binary Tree Zigzag Level Order Traversal - * - * https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/ - * - * algorithms - * Medium (40.57%) - * Total Accepted: 201.2K - * Total Submissions: 493.7K - * Testcase Example: '[3,9,20,null,null,15,7]' - * - * Given a binary tree, return the zigzag level order traversal of its nodes' - * values. (ie, from left to right, then right to left for the next level and - * alternate between). - * - * - * For example: - * Given binary tree [3,9,20,null,null,15,7], - * - * ⁠ 3 - * ⁠ / \ - * ⁠ 9 20 - * ⁠ / \ - * ⁠ 15 7 - * - * - * - * return its zigzag level order traversal as: - * - * [ - * ⁠ [3], - * ⁠ [20,9], - * ⁠ [15,7] - * ] - * - * - */ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ /** * @param {TreeNode} root * @return {number[][]} diff --git a/problems/104.maximum-depth-of-binary-tree.md b/problems/104.maximum-depth-of-binary-tree.md index 2d5c0c0e8..ab6ac41c9 100644 --- a/problems/104.maximum-depth-of-binary-tree.md +++ b/problems/104.maximum-depth-of-binary-tree.md @@ -177,8 +177,7 @@ class Solution: - [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md) - [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md b/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md index 9573b04f8..b3fef8ae8 100644 --- a/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md +++ b/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md @@ -1,6 +1,6 @@ ## 题目地址(105. 从前序与中序遍历序列构造二叉树) -https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ +https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ ## 题目描述 @@ -35,7 +35,7 @@ https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-tr - 百度 - 字节 -## 思路/Thinking Path +## 思路 目标是构造二叉树。 diff --git a/problems/108.convert-sorted-array-to-binary-search-tree.md b/problems/108.convert-sorted-array-to-binary-search-tree.md index 5c1128a06..e642a9f38 100644 --- a/problems/108.convert-sorted-array-to-binary-search-tree.md +++ b/problems/108.convert-sorted-array-to-binary-search-tree.md @@ -174,7 +174,7 @@ class Solution(object): - 时间复杂度:$O(N)$ - 空间复杂度:隐式调用栈的开销为 $O(N)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 公众号【 [力扣加加](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)】 知乎专栏【 [Lucifer - 知乎](https://www.zhihu.com/people/lu-xiao-13-70)】 diff --git a/problems/11.container-with-most-water.md b/problems/11.container-with-most-water.md index d62b9e812..6748c1c8f 100644 --- a/problems/11.container-with-most-water.md +++ b/problems/11.container-with-most-water.md @@ -3,29 +3,22 @@ https://leetcode-cn.com/problems/container-with-most-water/description/ ## 题目描述 +``` +给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点  (i, ai) 。在坐标内画 n 条垂直线,垂直线 i  的两个端点分别为  (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与  x  轴共同构成的容器可以容纳最多的水。 -给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 - -说明:你不能倾斜容器,且 n 的值至少为 2。 +说明:你不能倾斜容器,且  n  的值至少为 2。 -  ![11.container-with-most-water-question](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4wyztmj30m90anwep.jpg) +图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为  49。 -图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 -  示例: - +` 输入:[1,8,6,2,5,4,8,3,7] 输出:49 - - - - - - +``` ## 前置知识 - 双指针 @@ -41,7 +34,6 @@ https://leetcode-cn.com/problems/container-with-most-water/description/ 题目中说`找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。` ,因此符合直觉的解法就是固定两个端点,计算可以承载的水量, 然后不断更新最大值,最后返回最大值即可。这种算法,需要两层循环,时间复杂度是 $O(n^2)$。 - 代码(JS): ```js @@ -57,7 +49,7 @@ for (let i = 0; i < height.length; i++) { return max; ``` -虽然解法效率不高,但是可以通过(JS可以通过,Python 不可以,其他语言没有尝试)。那么有没有更优的解法呢? +虽然解法效率不高,但是可以通过(JS 可以通过,Python 不可以,其他语言没有尝试)。那么有没有更优的解法呢? 我们来换个角度来思考这个问题,上述的解法是通过两两组合,这无疑是完备的。我们换个角度思考,是否可以先计算长度为 n 的面积,然后计算长度为 n-1 的面积,... 计算长度为 1 的面积。 这样去不断更新最大值呢?很显然这种解法也是完备的,但是似乎时间复杂度还是 $O(n ^ 2)$, 不要着急。 @@ -152,9 +144,8 @@ class Solution: - 时间复杂度:由于左右指针移动的次数加起来正好是 n, 因此时间复杂度为 $O(N)$。 - 空间复杂度:$O(1)$。 -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 - ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4yqnsgj30p00dwt9t.jpg) diff --git a/problems/1104.path-in-zigzag-labelled-binary-tree.md b/problems/1104.path-in-zigzag-labelled-binary-tree.md index e68a3dbd6..fa770f0e1 100644 --- a/problems/1104.path-in-zigzag-labelled-binary-tree.md +++ b/problems/1104.path-in-zigzag-labelled-binary-tree.md @@ -57,7 +57,7 @@ https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/ ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5f240wj309b08dmxj.jpg) -因此我们只需要求解出每一层的这个定值,然后减去当前值就好了。(注意我们不需要区分偶数行和奇数行) +因此实际上只需要求解出每一层的这个定值,然后减去当前值就好了。(注意我们不需要区分偶数行和奇数行) 问题的关键转化为求解这个定值,这个定值其实很好求,因为每一层的最大值和最小值我们很容易求,而最大值和最小值的和正是我们要求的这个数字。 最大值和最小值这么求呢?由满二叉树的性质,我们知道每一层的最小值就是`2 ** (level - 1)`,而最大值是`2 ** level - 1`。 因此我们只要知道 level 即可,level 非常容易求出,具体可以看下面代码。 @@ -79,7 +79,6 @@ class Solution: def pathInZigZagTree(self, label: int) -> List[int]: level = 0 res = [] - # for each level, ranged from 2 ** (level - 1) to 2 ** level - 1 while 2 ** level - 1 < label: level += 1 @@ -90,3 +89,11 @@ class Solution: level -= 1 return res ``` + +**复杂度分析** +- 时间复杂度:由于每次都在头部插入 res,因此时间复杂度为 $O(log_Label)$, 一共插入了 $O(log_Label)$ 次, 因此总的时间复杂度为 $O(logLabel * logLabel)$。 +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1131.maximum-of-absolute-value-expression.md b/problems/1131.maximum-of-absolute-value-expression.md index dc53784a7..e3e7478dd 100644 --- a/problems/1131.maximum-of-absolute-value-expression.md +++ b/problems/1131.maximum-of-absolute-value-expression.md @@ -188,6 +188,11 @@ class Solution: return max(dists) ``` +**复杂度分析** + +- 时间复杂度:$O(N^3)$ +- 空间复杂度:$O(N)$ + ## 总结 可以看出其实两种解法都是一样的,只是思考角度不一样。 @@ -199,3 +204,7 @@ class Solution: ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2h4bnaj30xd0jzgom.jpg) - [1162. 地图分析](https://leetcode-cn.com/problems/as-far-from-land-as-possible/) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1168.optimize-water-distribution-in-a-village.md b/problems/1168.optimize-water-distribution-in-a-village.md index d64e07340..e42993c4e 100644 --- a/problems/1168.optimize-water-distribution-in-a-village.md +++ b/problems/1168.optimize-water-distribution-in-a-village.md @@ -72,9 +72,10 @@ pipes[i][0] != pipes[i][1] 从图中可以看到,最后所有的节点都是连通的。 -#### 复杂度分析 -- *时间复杂度:* `O(ElogE) - E 是图的边的个数` -- *空间复杂度:* `O(E)` +**复杂度分析** + +- 时间复杂度: `O(ElogE) - E 是图的边的个数` +- 空间复杂度: `O(E)` > 一个图最多有 `n(n-1)/2 - n 是图中节点个数` 条边 (完全连通图) diff --git a/problems/1186.maximum-subarray-sum-with-one-deletion.md b/problems/1186.maximum-subarray-sum-with-one-deletion.md index 69692e320..f2c947278 100644 --- a/problems/1186.maximum-subarray-sum-with-one-deletion.md +++ b/problems/1186.maximum-subarray-sum-with-one-deletion.md @@ -147,12 +147,12 @@ class Solution: max0 = max(max0 + arr[i], arr[i]) res = max(res, max0, max1) return res +``` +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ -# @lc code=end - - -``` ## 关键点解析 - 空间换时间 @@ -164,6 +164,11 @@ class Solution: - [42.trapping-rain-water](./42.trapping-rain-water.md) +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + + diff --git a/problems/121.best-time-to-buy-and-sell-stock.md b/problems/121.best-time-to-buy-and-sell-stock.md index 2a22d4a55..5fba3f07e 100644 --- a/problems/121.best-time-to-buy-and-sell-stock.md +++ b/problems/121.best-time-to-buy-and-sell-stock.md @@ -151,8 +151,7 @@ class Solution: - [122.best-time-to-buy-and-sell-stock-ii](./122.best-time-to-buy-and-sell-stock-ii.md) - [309.best-time-to-buy-and-sell-stock-with-cooldown](./309.best-time-to-buy-and-sell-stock-with-cooldown.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1218.longest-arithmetic-subsequence-of-given-difference.md b/problems/1218.longest-arithmetic-subsequence-of-given-difference.md index e97c7f158..11ea4ab10 100644 --- a/problems/1218.longest-arithmetic-subsequence-of-given-difference.md +++ b/problems/1218.longest-arithmetic-subsequence-of-given-difference.md @@ -45,7 +45,7 @@ https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-differe ## 思路 -最直观的思路是双层循环,我们暴力的枚举出以每一个元素为开始元素,以最后元素结尾的的所有情况。很明显这是所有的情况,这就是暴力法的精髓, 很明显这种解法会TLE(超时),不过我们先来看一下代码,顺着这个思维继续思考。 +最直观的思路是双层循环,我们暴力的枚举出以每一个元素为开始元素,以最后元素结尾的的所有情况。很明显这是所有的情况,这就是暴力法的精髓, 很明显这种解法会 TLE(超时),不过我们先来看一下代码,顺着这个思维继续思考。 ### 暴力法 @@ -64,20 +64,24 @@ https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-differe return res ``` -### 动态规划 -上面的时间复杂度是O(n^2), 有没有办法降低到O(n)呢?很容易想到的是空间换时间的解决方案。 +**复杂度分析** + +- 时间复杂度:$O(N^2)$ +- 空间复杂度:$O(N)$ + +### 动态规划 -我的想法是将`以每一个元素结尾的最长等差子序列的长度`统统存起来,即`dp[num] = maxLen` 这样我们遍历到一个新的元素的时候,就去之前的存储中去找`dp[num - difference]`, 如果找到了,就更新当前的`dp[num] = dp[num - difference] + 1`, 否则就是不进行操作(还是默认值1)。 +上面的时间复杂度是 O(n^2), 有没有办法降低到 O(n)呢?很容易想到的是空间换时间的解决方案。 -这种空间换时间的做法的时间和空间复杂度都是O(n)。 +我的想法是将`以每一个元素结尾的最长等差子序列的长度`统统存起来,即`dp[num] = maxLen` 这样我们遍历到一个新的元素的时候,就去之前的存储中去找`dp[num - difference]`, 如果找到了,就更新当前的`dp[num] = dp[num - difference] + 1`, 否则就是不进行操作(还是默认值 1)。 +这种空间换时间的做法的时间和空间复杂度都是 O(n)。 ## 关键点解析 - 将`以每一个元素结尾的最长等差子序列的长度`统统存起来 - ## 代码 ```python @@ -107,7 +111,11 @@ class Solution: # @lc code=end ``` -## 相关题目 - +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/122.best-time-to-buy-and-sell-stock-ii.md b/problems/122.best-time-to-buy-and-sell-stock-ii.md index 0f02f497c..dea9efc7b 100644 --- a/problems/122.best-time-to-buy-and-sell-stock-ii.md +++ b/problems/122.best-time-to-buy-and-sell-stock-ii.md @@ -139,8 +139,7 @@ class Solution { - [121.best-time-to-buy-and-sell-stock](./121.best-time-to-buy-and-sell-stock.md) - [309.best-time-to-buy-and-sell-stock-with-cooldown](./309.best-time-to-buy-and-sell-stock-with-cooldown.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1227.airplane-seat-assignment-probability.md b/problems/1227.airplane-seat-assignment-probability.md index b89367dd1..1272bb04b 100644 --- a/problems/1227.airplane-seat-assignment-probability.md +++ b/problems/1227.airplane-seat-assignment-probability.md @@ -223,6 +223,11 @@ class Solution: return dp[-1] ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 优化数学分析 ### 思路 @@ -248,6 +253,11 @@ class Solution: ``` +**复杂度分析** + +- 时间复杂度:$O(1)$ +- 空间复杂度:$O(1)$ + ## 关键点 - 概率分析 @@ -256,3 +266,7 @@ class Solution: - 递归 + mapper - 栈限制大小 - 尾递归 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/124.binary-tree-maximum-path-sum.md b/problems/124.binary-tree-maximum-path-sum.md index a70490964..65fb130b6 100644 --- a/problems/124.binary-tree-maximum-path-sum.md +++ b/problems/124.binary-tree-maximum-path-sum.md @@ -1,6 +1,6 @@ - ## 题目地址(124. 二叉树中的最大路径和) -https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ + +https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/description/ ## 题目描述 @@ -10,8 +10,6 @@ https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ 本题中,路径被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。 -  - 示例 1: 输入:[1,2,3] @@ -48,23 +46,22 @@ https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ ## 思路 - 这道题目的path让我误解了,然后浪费了很多时间来解这道题 - 我觉得leetcode给的demo太少了,不足以让我理解path的概念 - 因此我这里自己画了一个图,来补充一下,帮助大家理解path的概念,不要像我一样理解错啦。 +这道题目的 path 让我误解了,然后浪费了很多时间来解这道题 +我觉得 leetcode 给的 demo 太少了,不足以让我理解 path 的概念 +因此我这里自己画了一个图,来补充一下,帮助大家理解 path 的概念,不要像我一样理解错啦。 - 首先是官网给的两个例子: +首先是官网给的两个例子: - ![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaht4drj30kh07pq3p.jpg) +![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaht4drj30kh07pq3p.jpg) - 接着是我自己画的一个例子: +接着是我自己画的一个例子: - ![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluai4m6dj30hu0cdq46.jpg) +![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluai4m6dj30hu0cdq46.jpg) -如图红色的部分是最大路径上的节点。大家可以结合上面的demo来继续理解一下path, 除非你理解了path,否则不要往下看。 +如图红色的部分是最大路径上的节点。大家可以结合上面的 demo 来继续理解一下 path, 除非你理解了 path,否则不要往下看。 + +树的题目,基本都是考察递归思想的。因此我们需要思考如何去定义我们的递归函数,在这里我定义了一个递归函数,它的功能是,`返回以当前节点为根节点的MaxPath` - - 树的题目,基本都是考察递归思想的。因此我们需要思考如何去定义我们的递归函数,在这里我定义了一个递归函数,它的功能是,`返回以当前节点为根节点的MaxPath` - 但是有两个条件: 1. 根节点必须选择 @@ -81,7 +78,7 @@ https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ ## 关键点解析 - 递归 -- 理解题目中的path定义 +- 理解题目中的 path 定义 ## 代码 @@ -90,8 +87,6 @@ https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ - JavaScript ```js - - /* * @lc app=leetcode id=124 lang=javascript * @@ -121,10 +116,10 @@ function helper(node, payload) { * @param {TreeNode} root * @return {number} */ -var maxPathSum = function(root) { +var maxPathSum = function (root) { if (root === null) return 0; const payload = { - max: root.val + max: root.val, }; helper(root, payload); return payload.max; @@ -176,8 +171,17 @@ class Solution: return max(l, r, 0) + node.val helper(root) return self.ans - ``` +``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ ## 相关题目 + - [113.path-sum-ii](./113.path-sum-ii.md) +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/125.valid-palindrome.md b/problems/125.valid-palindrome.md index 3b72d5379..438f92539 100644 --- a/problems/125.valid-palindrome.md +++ b/problems/125.valid-palindrome.md @@ -165,8 +165,7 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1260.shift-2d-grid.md b/problems/1260.shift-2d-grid.md index 9c5af2f5b..70a3d7c6d 100644 --- a/problems/1260.shift-2d-grid.md +++ b/problems/1260.shift-2d-grid.md @@ -159,7 +159,7 @@ class Solution: - [文科生都能看懂的循环移位算法](https://lucifer.ren/blog/2019/12/11/rotate-list/) -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/1262.greatest-sum-divisible-by-three.md b/problems/1262.greatest-sum-divisible-by-three.md index f49b88d23..92389d2ad 100644 --- a/problems/1262.greatest-sum-divisible-by-three.md +++ b/problems/1262.greatest-sum-divisible-by-three.md @@ -241,6 +241,11 @@ class Solution: return state[0] ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + ## 关键点解析 - 贪婪法 @@ -253,4 +258,6 @@ class Solution: 另外如果题目改成了`请你找出并返回能被x整除的元素最大和`,你只需要将我的解法中的 3 改成 x 即可。 - +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/128.longest-consecutive-sequence.md b/problems/128.longest-consecutive-sequence.md index 0b1394886..834fe26c7 100644 --- a/problems/128.longest-consecutive-sequence.md +++ b/problems/128.longest-consecutive-sequence.md @@ -93,3 +93,12 @@ var longestConsecutive = function(nums) { return max; }; ``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/129.sum-root-to-leaf-numbers.md b/problems/129.sum-root-to-leaf-numbers.md index 464f10196..c2ced8268 100644 --- a/problems/129.sum-root-to-leaf-numbers.md +++ b/problems/129.sum-root-to-leaf-numbers.md @@ -168,6 +168,11 @@ class Solution: return helper(root, 0) ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 拓展 通常来说,可以利用队列、栈等数据结构将递归算法转为递推算法。 @@ -252,3 +257,7 @@ class Solution: - [sum-of-root-to-leaf-binary-numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/) > 这道题和本题太像了,跟一道题没啥区别 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1297.maximum-number-of-occurrences-of-a-substring.md b/problems/1297.maximum-number-of-occurrences-of-a-substring.md index 3510e0caa..47ab830d7 100644 --- a/problems/1297.maximum-number-of-occurrences-of-a-substring.md +++ b/problems/1297.maximum-number-of-occurrences-of-a-substring.md @@ -142,6 +142,13 @@ public boolean checkNum(String substr, int maxLetters) { ``` +**复杂度分析** + +其中 N 为 s 长度 + +- 时间复杂度:$O(N * minSize)$ +- 空间复杂度:$O(N * minSize)$ + ## 关键点解析 - 滑动窗口 @@ -151,3 +158,7 @@ public boolean checkNum(String substr, int maxLetters) { ## 扩展 我们也可以使用滑动窗口来解决,感兴趣的可以试试看。 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/130.surrounded-regions.md b/problems/130.surrounded-regions.md index bb2a92958..82240207a 100644 --- a/problems/130.surrounded-regions.md +++ b/problems/130.surrounded-regions.md @@ -171,7 +171,7 @@ class Solution: - 时间复杂度:$O(row * col)$ - 空间复杂度:$O(row * col)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/1310.xor-queries-of-a-subarray.md b/problems/1310.xor-queries-of-a-subarray.md index 0ebd79f06..bd7f9b6e7 100644 --- a/problems/1310.xor-queries-of-a-subarray.md +++ b/problems/1310.xor-queries-of-a-subarray.md @@ -162,6 +162,13 @@ public: }; ``` +**复杂度分析** + +其中 N 为数组 arr 长度, M 为 queries 的长度。 + +- 时间复杂度:$O(N * M)$ +- 空间复杂度:$O(N)$ + ## 关键点解析 - 异或的性质 x ^ y ^ x = y @@ -174,3 +181,8 @@ public: ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxt83dtj30u00ftac4.jpg) - [1186.删除一次得到子数组最大和](https://lucifer.ren/blog/2019/12/11/leetcode-1186/) + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1332.remove-palindromic-subsequences.md b/problems/1332.remove-palindromic-subsequences.md index 3e2870b9c..508f75031 100644 --- a/problems/1332.remove-palindromic-subsequences.md +++ b/problems/1332.remove-palindromic-subsequences.md @@ -109,7 +109,7 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md b/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md index 173a78f86..f78487d6d 100644 --- a/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md +++ b/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md @@ -121,7 +121,16 @@ class Solution: ``` +**复杂度分析** + +- 时间复杂度:$O(N^3)$ +- 空间复杂度:$O(N^2)$ + ## 关键点解析 - Floyd-Warshall 算法 - 你可以将本文给的 Floyd-Warshall 算法当成一种解题模板使用 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/136.single-number.md b/problems/136.single-number.md index b4c5fd19d..6cefeadaf 100644 --- a/problems/136.single-number.md +++ b/problems/136.single-number.md @@ -60,43 +60,8 @@ https://leetcode-cn.com/problems/single-number/ * 语言支持:JS,C,C++,Java,Python JavaScrip Code: + ```js -/* - * @lc app=leetcode id=136 lang=javascript - * - * [136] Single Number - * - * https://leetcode.com/problems/single-number/description/ - * - * algorithms - * Easy (59.13%) - * Total Accepted: 429.3K - * Total Submissions: 724.1K - * Testcase Example: '[2,2,1]' - * - * Given a non-empty array of integers, every element appears twice except for - * one. Find that single one. - * - * Note: - * - * Your algorithm should have a linear runtime complexity. Could you implement - * it without using extra memory? - * - * Example 1: - * - * - * Input: [2,2,1] - * Output: 1 - * - * - * Example 2: - * - * - * Input: [4,1,2,1,2] - * Output: 4 - * - * - */ /** * @param {number[]} nums * @return {number} @@ -110,7 +75,9 @@ var singleNumber = function(nums) { return ret; }; ``` + C Code: + ```C int singleNumber(int* nums, int numsSize){ int res=0; @@ -199,7 +166,7 @@ class Solution: Done! -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/139.word-break.md b/problems/139.word-break.md index 9e9f89df0..0601050d4 100644 --- a/problems/139.word-break.md +++ b/problems/139.word-break.md @@ -76,58 +76,6 @@ https://leetcode-cn.com/problems/word-break/ ## 代码 ```js -/* - * @lc app=leetcode id=139 lang=javascript - * - * [139] Word Break - * - * https://leetcode.com/problems/word-break/description/ - * - * algorithms - * Medium (34.45%) - * Total Accepted: 317.8K - * Total Submissions: 913.9K - * Testcase Example: '"leetcode"\n["leet","code"]' - * - * Given a non-empty string s and a dictionary wordDict containing a list of - * non-empty words, determine if s can be segmented into a space-separated - * sequence of one or more dictionary words. - * - * Note: - * - * - * The same word in the dictionary may be reused multiple times in the - * segmentation. - * You may assume the dictionary does not contain duplicate words. - * - * - * Example 1: - * - * - * Input: s = "leetcode", wordDict = ["leet", "code"] - * Output: true - * Explanation: Return true because "leetcode" can be segmented as "leet - * code". - * - * - * Example 2: - * - * - * Input: s = "applepenapple", wordDict = ["apple", "pen"] - * Output: true - * Explanation: Return true because "applepenapple" can be segmented as "apple - * pen apple". - * Note that you are allowed to reuse a dictionary word. - * - * - * Example 3: - * - * - * Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] - * Output: false - * - * - */ /** * @param {string} s * @param {string[]} wordDict @@ -149,3 +97,12 @@ var wordBreak = function (s, wordDict) { return dp[s.length] || false; }; ``` + +**复杂度分析** + +- 时间复杂度:$O(N ^ 2)$ +- 空间复杂度:$O(N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/144.binary-tree-preorder-traversal.md b/problems/144.binary-tree-preorder-traversal.md index 3f6f8a069..7f41082b3 100644 --- a/problems/144.binary-tree-preorder-traversal.md +++ b/problems/144.binary-tree-preorder-traversal.md @@ -132,7 +132,15 @@ public: } }; ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ ## 相关专题 - [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md b/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md index 920be35fe..f3e98784e 100644 --- a/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md +++ b/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md @@ -172,7 +172,7 @@ class Solution: ``` -**_复杂度分析_** +**复杂度分析** - 时间复杂度:$O(target))$ - 空间复杂度:$O(target)$ @@ -217,8 +217,8 @@ for j in V to 0: **因此总的来说,如果你认为[1,3]和[3,1]是一种,那么就用方法 1 的遍历,否则用方法 2。** -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 - -大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluc9tkv6j30x20iwjtf.jpg) diff --git a/problems/145.binary-tree-postorder-traversal.md b/problems/145.binary-tree-postorder-traversal.md index 8dcae4771..25ebd5ea9 100644 --- a/problems/145.binary-tree-postorder-traversal.md +++ b/problems/145.binary-tree-postorder-traversal.md @@ -113,8 +113,17 @@ var postorderTraversal = function(root) { ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 相关专题 - [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md) +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + diff --git a/problems/15.3sum.md b/problems/15.3sum.md index 585eb5931..e0b182080 100644 --- a/problems/15.3sum.md +++ b/problems/15.3sum.md @@ -60,40 +60,6 @@ https://leetcode-cn.com/problems/3sum/ ## 代码 ```js -/* - * @lc app=leetcode id=15 lang=javascript - * - * [15] 3Sum - * - * https://leetcode.com/problems/3sum/description/ - * - * algorithms - * Medium (23.51%) - * Total Accepted: 531.5K - * Total Submissions: 2.2M - * Testcase Example: '[-1,0,1,2,-1,-4]' - * - * Given an array nums of n integers, are there elements a, b, c in nums such - * that a + b + c = 0? Find all unique triplets in the array which gives the - * sum of zero. - * - * Note: - * - * The solution set must not contain duplicate triplets. - * - * Example: - * - * - * Given array nums = [-1, 0, 1, 2, -1, -4], - * - * A solution set is: - * [ - * ⁠ [-1, 0, 1], - * ⁠ [-1, -1, 2] - * ] - * - * - */ /** * @param {number[]} nums * @return {number[][]} @@ -137,3 +103,11 @@ var threeSum = function (nums) { return list; }; ``` + +**复杂度分析** +- 时间复杂度:$O(N^2)$ +- 空间复杂度:取决于排序算法的空间消耗 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/150.evaluate-reverse-polish-notation.md b/problems/150.evaluate-reverse-polish-notation.md index 0da04ae1b..4bc910fea 100644 --- a/problems/150.evaluate-reverse-polish-notation.md +++ b/problems/150.evaluate-reverse-polish-notation.md @@ -86,65 +86,6 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ ## 代码 ```js -/* - * @lc app=leetcode id=150 lang=javascript - * - * [150] Evaluate Reverse Polish Notation - * - * https://leetcode.com/problems/evaluate-reverse-polish-notation/description/ - * - * algorithms - * Medium (31.43%) - * Total Accepted: 153.3K - * Total Submissions: 485.8K - * Testcase Example: '["2","1","+","3","*"]' - * - * Evaluate the value of an arithmetic expression in Reverse Polish Notation. - * - * Valid operators are +, -, *, /. Each operand may be an integer or another - * expression. - * - * Note: - * - * - * Division between two integers should truncate toward zero. - * The given RPN expression is always valid. That means the expression would - * always evaluate to a result and there won't be any divide by zero - * operation. - * - * - * Example 1: - * - * - * Input: ["2", "1", "+", "3", "*"] - * Output: 9 - * Explanation: ((2 + 1) * 3) = 9 - * - * - * Example 2: - * - * - * Input: ["4", "13", "5", "/", "+"] - * Output: 6 - * Explanation: (4 + (13 / 5)) = 6 - * - * - * Example 3: - * - * - * Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] - * Output: 22 - * Explanation: - * ⁠ ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 - * = ((10 * (6 / (12 * -11))) + 17) + 5 - * = ((10 * (6 / -132)) + 17) + 5 - * = ((10 * 0) + 17) + 5 - * = (0 + 17) + 5 - * = 17 + 5 - * = 22 - * - * - */ /** * @param {string[]} tokens * @return {number} diff --git a/problems/155.min-stack.md b/problems/155.min-stack.md index 60030010a..1cb54eeef 100644 --- a/problems/155.min-stack.md +++ b/problems/155.min-stack.md @@ -555,7 +555,7 @@ class MinStack: - 时间复杂度:O(1) - 空间复杂度:O(1) -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/169.majority-element.md b/problems/169.majority-element.md index 0e0c992a3..b1ea9fabf 100644 --- a/problems/169.majority-element.md +++ b/problems/169.majority-element.md @@ -93,7 +93,7 @@ class Solution: - 时间复杂度:$O(N)$,其中N为数组长度 - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/17.Letter-Combinations-of-a-Phone-Number.md b/problems/17.Letter-Combinations-of-a-Phone-Number.md index 6ff31a599..7a8612bd0 100644 --- a/problems/17.Letter-Combinations-of-a-Phone-Number.md +++ b/problems/17.Letter-Combinations-of-a-Phone-Number.md @@ -89,9 +89,13 @@ const letterCombinations = function (digits) { }; ``` -**_复杂度分析_** +**复杂度分析** N + M 是输入数字的总数 - 时间复杂度:O(3^N \* 4^M) - 空间复杂度:O(3^N \* 4^M) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/172.factorial-trailing-zeroes.md b/problems/172.factorial-trailing-zeroes.md index 85e89f373..ce5ebb6dc 100644 --- a/problems/172.factorial-trailing-zeroes.md +++ b/problems/172.factorial-trailing-zeroes.md @@ -110,7 +110,7 @@ class Solution: - 时间复杂度:$O(logN)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/19.removeNthNodeFromEndofList.md b/problems/19.removeNthNodeFromEndofList.md index 43c89ed19..31220dccc 100644 --- a/problems/19.removeNthNodeFromEndofList.md +++ b/problems/19.removeNthNodeFromEndofList.md @@ -69,47 +69,6 @@ Support: JS, Java - Javascript Implementation ```js -/* - * @lc app=leetcode id=19 lang=javascript - * - * [19] Remove Nth Node From End of List - * - * https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ - * - * algorithms - * Medium (34.03%) - * Total Accepted: 360.1K - * Total Submissions: 1.1M - * Testcase Example: '[1,2,3,4,5]\n2' - * - * Given a linked list, remove the n-th node from the end of list and return - * its head. - * - * Example: - * - * - * Given linked list: 1->2->3->4->5, and n = 2. - * - * After removing the second node from the end, the linked list becomes - * 1->2->3->5. - * - * - * Note: - * - * Given n will always be valid. - * - * Follow up: - * - * Could you do this in one pass? - * - */ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ /** * @param {ListNode} head * @param {number} n @@ -178,3 +137,11 @@ class Solution { } } ``` + +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/190.reverse-bits.md b/problems/190.reverse-bits.md index f41268a05..b2eddb89e 100644 --- a/problems/190.reverse-bits.md +++ b/problems/190.reverse-bits.md @@ -81,63 +81,6 @@ eg : JavaScript Code: ```js -/* - * @lc app=leetcode id=190 lang=javascript - * - * [190] Reverse Bits - * - * https://leetcode.com/problems/reverse-bits/description/ - * - * algorithms - * Easy (30.30%) - * Total Accepted: 173.7K - * Total Submissions: 568.2K - * Testcase Example: '00000010100101000001111010011100' - * - * Reverse bits of a given 32 bits unsigned integer. - * - * - * - * Example 1: - * - * - * Input: 00000010100101000001111010011100 - * Output: 00111001011110000010100101000000 - * Explanation: The input binary string 00000010100101000001111010011100 - * represents the unsigned integer 43261596, so return 964176192 which its - * binary representation is 00111001011110000010100101000000. - * - * - * Example 2: - * - * - * Input: 11111111111111111111111111111101 - * Output: 10111111111111111111111111111111 - * Explanation: The input binary string 11111111111111111111111111111101 - * represents the unsigned integer 4294967293, so return 3221225471 which its - * binary representation is 10101111110010110010011101101001. - * - * - * - * Note: - * - * - * Note that in some languages such as Java, there is no unsigned integer type. - * In this case, both input and output will be given as signed integer type and - * should not affect your implementation, as the internal binary representation - * of the integer is the same whether it is signed or unsigned. - * In Java, the compiler represents the signed integers using 2's complement - * notation. Therefore, in Example 2 above the input represents the signed - * integer -3 and the output represents the signed integer -1073741825. - * - * - * - * - * Follow up: - * - * If this function is called many times, how would you optimize it? - * - */ /** * @param {number} n - a positive integer * @return {number} - a positive integer @@ -209,7 +152,7 @@ public: }; ``` -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/191.number-of-1-bits.md b/problems/191.number-of-1-bits.md index bf53073d7..f8356f547 100644 --- a/problems/191.number-of-1-bits.md +++ b/problems/191.number-of-1-bits.md @@ -161,7 +161,7 @@ public: }; ``` -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/198.house-robber.md b/problems/198.house-robber.md index 2c01c8ae7..9764553f7 100644 --- a/problems/198.house-robber.md +++ b/problems/198.house-robber.md @@ -103,49 +103,6 @@ return b; JavaScript Code: ```js -/* - * @lc app=leetcode id=198 lang=javascript - * - * [198] House Robber - * - * https://leetcode.com/problems/house-robber/description/ - * - * algorithms - * Easy (40.80%) - * Total Accepted: 312.1K - * Total Submissions: 762.4K - * Testcase Example: '[1,2,3,1]' - * - * You are a professional robber planning to rob houses along a street. Each - * house has a certain amount of money stashed, the only constraint stopping - * you from robbing each of them is that adjacent houses have security system - * connected and it will automatically contact the police if two adjacent - * houses were broken into on the same night. - * - * Given a list of non-negative integers representing the amount of money of - * each house, determine the maximum amount of money you can rob tonight - * without alerting the police. - * - * Example 1: - * - * - * Input: [1,2,3,1] - * Output: 4 - * Explanation: Rob house 1 (money = 1) and then rob house 3 (money = - * 3). - * Total amount you can rob = 1 + 3 = 4. - * - * Example 2: - * - * - * Input: [2,7,9,3,1] - * Output: 12 - * Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house - * 5 (money = 1). - * Total amount you can rob = 2 + 9 + 1 = 12. - * - * - */ /** * @param {number[]} nums * @return {number} @@ -215,8 +172,7 @@ class Solution: - [337.house-robber-iii](https://github.com/azl397985856/leetcode/blob/master/problems/337.house-robber-iii.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/199.binary-tree-right-side-view.md b/problems/199.binary-tree-right-side-view.md index 099ae1cee..a816fcfeb 100644 --- a/problems/199.binary-tree-right-side-view.md +++ b/problems/199.binary-tree-right-side-view.md @@ -54,47 +54,11 @@ https://leetcode-cn.com/problems/binary-tree-right-side-view/ ## 代码 -* 语言支持:JS,C++ +语言支持:JS,C++ Javascript Code: + ```js -/* - * @lc app=leetcode id=199 lang=javascript - * - * [199] Binary Tree Right Side View - * - * https://leetcode.com/problems/binary-tree-right-side-view/description/ - * - * algorithms - * Medium (46.74%) - * Total Accepted: 156.1K - * Total Submissions: 332.3K - * Testcase Example: '[1,2,3,null,5,null,4]' - * - * Given a binary tree, imagine yourself standing on the right side of it, - * return the values of the nodes you can see ordered from top to bottom. - * - * Example: - * - * - * Input: [1,2,3,null,5,null,4] - * Output: [1, 3, 4] - * Explanation: - * - * ⁠ 1 <--- - * ⁠/ \ - * 2 3 <--- - * ⁠\ \ - * ⁠ 5 4 <--- - * - */ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ /** * @param {TreeNode} root * @return {number[]} diff --git a/problems/2.add-two-numbers.md b/problems/2.add-two-numbers.md index 60fa05bb9..35de59a0a 100644 --- a/problems/2.add-two-numbers.md +++ b/problems/2.add-two-numbers.md @@ -139,6 +139,11 @@ public: }; ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + ## 拓展 通过单链表的定义可以得知,单链表也是递归结构,因此,也可以使用递归的方式来进行 reverse 操作。 @@ -197,3 +202,11 @@ private: } }; ``` + +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$,其中 N 的空间是调用栈的开销。 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/20.valid-parentheses.md b/problems/20.valid-parentheses.md index 2362420f3..979f985dd 100644 --- a/problems/20.valid-parentheses.md +++ b/problems/20.valid-parentheses.md @@ -250,7 +250,7 @@ var isValid = function (s) { - 如果让你检查 XML 标签是否闭合如何检查, 更进一步如果要你实现一个简单的 XML 的解析器,应该怎么实现? -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/203.remove-linked-list-elements.md b/problems/203.remove-linked-list-elements.md index 602dba782..f1f9bde26 100644 --- a/problems/203.remove-linked-list-elements.md +++ b/problems/203.remove-linked-list-elements.md @@ -104,7 +104,7 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/206.reverse-linked-list.md b/problems/206.reverse-linked-list.md index 9376b81e6..4206e1620 100644 --- a/problems/206.reverse-linked-list.md +++ b/problems/206.reverse-linked-list.md @@ -266,7 +266,7 @@ class Solution: - [92.reverse-linked-list-ii](./92.reverse-linked-list-ii.md) - [25.reverse-nodes-in-k-groups](./25.reverse-nodes-in-k-groups-cn.md) -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/209.minimum-size-subarray-sum.md b/problems/209.minimum-size-subarray-sum.md index b0023bbda..34ef919ef 100644 --- a/problems/209.minimum-size-subarray-sum.md +++ b/problems/209.minimum-size-subarray-sum.md @@ -170,4 +170,8 @@ var minSubArrayLen = function(s, nums) { }; ``` +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + diff --git a/problems/21.merge-two-sorted-lists.md b/problems/21.merge-two-sorted-lists.md index 4765b7bf6..51369514a 100644 --- a/problems/21.merge-two-sorted-lists.md +++ b/problems/21.merge-two-sorted-lists.md @@ -83,8 +83,7 @@ M、N 是两条链表 l1、l2 的长度 - 时间复杂度:$O(M+N)$ - 空间复杂度:$O(M+N)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/219.contains-duplicate-ii.md b/problems/219.contains-duplicate-ii.md index f68fb3e9b..c2dcc0acf 100644 --- a/problems/219.contains-duplicate-ii.md +++ b/problems/219.contains-duplicate-ii.md @@ -155,7 +155,7 @@ public: - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/22.generate-parentheses.md b/problems/22.generate-parentheses.md index 974fa21c7..52fee2da1 100644 --- a/problems/22.generate-parentheses.md +++ b/problems/22.generate-parentheses.md @@ -78,7 +78,12 @@ const generateParenthesis = function (n) { }; ``` -**_复杂度分析_** +**复杂度分析** - 时间复杂度:O(2^N) - 空间复杂度:O(2^N) + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) \ No newline at end of file diff --git a/problems/226.invert-binary-tree.md b/problems/226.invert-binary-tree.md index 02465371d..e02640dac 100644 --- a/problems/226.invert-binary-tree.md +++ b/problems/226.invert-binary-tree.md @@ -156,7 +156,7 @@ public: - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/229.majority-element-ii.md b/problems/229.majority-element-ii.md index 20f442894..b6bd3a9ab 100644 --- a/problems/229.majority-element-ii.md +++ b/problems/229.majority-element-ii.md @@ -180,7 +180,10 @@ class Solution { } ``` +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ ## 扩展 @@ -189,3 +192,7 @@ class Solution { 大家可以自己思考一下,我这里给一个参考链接:https://leetcode.com/problems/majority-element-ii/discuss/63500/JAVA-Easy-Version-To-Understand!!!!!!!!!!!!/64925 这个实现说实话不是很好,大家可以优化一下。 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/23.merge-k-sorted-lists.md b/problems/23.merge-k-sorted-lists.md index e4af17d00..e5c538fc5 100644 --- a/problems/23.merge-k-sorted-lists.md +++ b/problems/23.merge-k-sorted-lists.md @@ -105,7 +105,7 @@ function mergeTwoLists(l1, l2) { * @param {ListNode[]} lists * @return {ListNode} */ -var mergeKLists = function(lists) { +var mergeKLists = function (lists) { // 图参考: https://zhuanlan.zhihu.com/p/61796021 if (lists.length === 0) return null; if (lists.length === 1) return lists[0]; @@ -130,7 +130,7 @@ var mergeKLists = function(lists) { Python3 Code: -``` python +```python # Definition for singly-linked list. # class ListNode: # def __init__(self, x): @@ -140,17 +140,17 @@ Python3 Code: class Solution: def mergeKLists(self, lists: List[ListNode]) -> ListNode: n = len(lists) - + # basic cases if lenth == 0: return None if lenth == 1: return lists[0] if lenth == 2: return self.mergeTwoLists(lists[0], lists[1]) - + # divide and conqure if not basic cases mid = n // 2 return self.mergeTwoLists(self.mergeKLists(lists[:mid]), self.mergeKLists(lists[mid:n])) - - + + def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: res = ListNode(0) c1, c2, c3 = l1, l2, res @@ -169,10 +169,19 @@ class Solution: else: c3.next = c2 break - + return res.next ``` +**复杂度分析** + +- 时间复杂度:$O(kn*logk)$ +- 空间复杂度:$O(logk)$ + ## 相关题目 - [88.merge-sorted-array](./88.merge-sorted-array.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/230.kth-smallest-element-in-a-bst.md b/problems/230.kth-smallest-element-in-a-bst.md index 05c4277ae..f6b003463 100644 --- a/problems/230.kth-smallest-element-in-a-bst.md +++ b/problems/230.kth-smallest-element-in-a-bst.md @@ -190,6 +190,11 @@ var kthSmallest = function(root, k) { ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 扩展 这道题有一个follow up: @@ -197,5 +202,9 @@ var kthSmallest = function(root, k) { `What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?` -大家可以思考一下。 +建议大家思考一下。 + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/232.implement-queue-using-stacks.md b/problems/232.implement-queue-using-stacks.md index 9d5a4f681..e8653e80b 100644 --- a/problems/232.implement-queue-using-stacks.md +++ b/problems/232.implement-queue-using-stacks.md @@ -275,7 +275,7 @@ class MyQueue { -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/236.lowest-common-ancestor-of-a-binary-tree.md b/problems/236.lowest-common-ancestor-of-a-binary-tree.md index a2ea46a25..668f5f47e 100644 --- a/problems/236.lowest-common-ancestor-of-a-binary-tree.md +++ b/problems/236.lowest-common-ancestor-of-a-binary-tree.md @@ -136,6 +136,15 @@ class Solution: ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 扩展 如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是什么意思,对结果有什么样的影响? + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/239.sliding-window-maximum.md b/problems/239.sliding-window-maximum.md index 8be4290cd..eb64ad970 100644 --- a/problems/239.sliding-window-maximum.md +++ b/problems/239.sliding-window-maximum.md @@ -160,8 +160,17 @@ class Solution: ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 扩展 ### 为什么用双端队列 -因为删除无效元素的时候,会清除队首的元素(索引太小了 -)或者队尾(元素太小了)的元素。 因此需要同时对队首和队尾进行操作,使用双端队列是一种合乎情理的做法。 +因为删除无效元素的时候,会清除队首的元素(索引太小了)或者队尾(元素太小了)的元素。 因此需要同时对队首和队尾进行操作,使用双端队列是一种合乎情理的做法。 + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/24.swapNodesInPairs.md b/problems/24.swapNodesInPairs.md index 929c9a3c0..a5b89add1 100644 --- a/problems/24.swapNodesInPairs.md +++ b/problems/24.swapNodesInPairs.md @@ -126,3 +126,11 @@ class Solution: _next.next = head return _next ``` + +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/240.search-a-2-d-matrix-ii.md b/problems/240.search-a-2-d-matrix-ii.md index 925ae4fbc..0b3f359dd 100644 --- a/problems/240.search-a-2-d-matrix-ii.md +++ b/problems/240.search-a-2-d-matrix-ii.md @@ -50,8 +50,7 @@ https://leetcode-cn.com/problems/search-a-2d-matrix-ii/ ## 关键点解析 -- 从角落开始遍历,利用递增的特性简化时间复杂度 - +- 从角落开始遍历,利用递增的特性简化时间复杂 ## 代码 @@ -122,3 +121,12 @@ class Solution: return False ``` +**复杂度分析** + +- 时间复杂度:$O(M + N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + diff --git a/problems/25.reverse-nodes-in-k-groups.md b/problems/25.reverse-nodes-in-k-groups.md index 73b705aea..c3c692cc8 100644 --- a/problems/25.reverse-nodes-in-k-groups.md +++ b/problems/25.reverse-nodes-in-k-groups.md @@ -324,7 +324,15 @@ class Solution: ``` +**复杂度分析** +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + ## 相关题目 - [92.reverse-linked-list-ii](./92.reverse-linked-list-ii.md) - [206.reverse-linked-list](./206.reverse-linked-list.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/26.remove-duplicates-from-sorted-array.md b/problems/26.remove-duplicates-from-sorted-array.md index 9e399dc55..a08ded529 100644 --- a/problems/26.remove-duplicates-from-sorted-array.md +++ b/problems/26.remove-duplicates-from-sorted-array.md @@ -154,7 +154,7 @@ public: - 时间复杂度:$O(N)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/263.ugly-number.md b/problems/263.ugly-number.md index bd8ff3580..c13ffa93b 100644 --- a/problems/263.ugly-number.md +++ b/problems/263.ugly-number.md @@ -164,7 +164,7 @@ class Solution: - 时间复杂度:$O(logN)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/279.perfect-squares.md b/problems/279.perfect-squares.md index 8222b05d3..d5431fe7b 100644 --- a/problems/279.perfect-squares.md +++ b/problems/279.perfect-squares.md @@ -99,36 +99,6 @@ for (let i = 1; i <= n; i++) { ## 代码 ```js -/* - * @lc app=leetcode id=279 lang=javascript - * - * [279] Perfect Squares - * - * https://leetcode.com/problems/perfect-squares/description/ - * - * algorithms - * Medium (40.98%) - * Total Accepted: 168.2K - * Total Submissions: 408.5K - * Testcase Example: '12' - * - * Given a positive integer n, find the least number of perfect square numbers - * (for example, 1, 4, 9, 16, ...) which sum to n. - * - * Example 1: - * - * - * Input: n = 12 - * Output: 3 - * Explanation: 12 = 4 + 4 + 4. - * - * Example 2: - * - * - * Input: n = 13 - * Output: 2 - * Explanation: 13 = 4 + 9. - */ /** * @param {number} n * @return {number} @@ -150,3 +120,12 @@ var numSquares = function(n) { return dp[n]; }; ``` + +**复杂度分析** + +- 时间复杂度:$O(N^2)$ +- 空间复杂度:$O(N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/283.move-zeroes.md b/problems/283.move-zeroes.md index f0eaa7fef..d6530f03f 100644 --- a/problems/283.move-zeroes.md +++ b/problems/283.move-zeroes.md @@ -139,7 +139,7 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/29.divide-two-integers.md b/problems/29.divide-two-integers.md index 8e1908539..ded6feff7 100644 --- a/problems/29.divide-two-integers.md +++ b/problems/29.divide-two-integers.md @@ -69,7 +69,7 @@ https://leetcode-cn.com/problems/divide-two-integers/ ## 关键点解析 -- 二分查找 +- [二分查找](../91/binary-search.md) - 正负数的判断中,这样判断更简单。 @@ -77,6 +77,12 @@ https://leetcode-cn.com/problems/divide-two-integers/ const isNegative = dividend > 0 !== divisor > 0; ``` +或者利用异或: + +```js +const isNegative = dividend ^ divisor < 0; +``` + ## 代码 @@ -185,5 +191,13 @@ class Solution: return result, dividend ``` +**复杂度分析** +- 时间复杂度:$O(logN)$ +- 空间复杂度:$O(1)$ + ## 相关题目 - [875.koko-eating-bananas](./875.koko-eating-bananas.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/295.find-median-from-data-stream.md b/problems/295.find-median-from-data-stream.md index e0fb6e240..ed235b800 100644 --- a/problems/295.find-median-from-data-stream.md +++ b/problems/295.find-median-from-data-stream.md @@ -224,63 +224,6 @@ this.heap.shift(null); > 关于PriorityQueue的实现,感兴趣的可以看下 https://github.com/janogonzalez/priorityqueuejs ```js -/* - * @lc app=leetcode id=295 lang=javascript - * - * [295] Find Median from Data Stream - * - * https://leetcode.com/problems/find-median-from-data-stream/description/ - * - * algorithms - * Hard (35.08%) - * Total Accepted: 101.2K - * Total Submissions: 282.4K - * Testcase Example: '["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"]\n[[],[1],[2],[],[3],[]]' - * - * Median is the middle value in an ordered integer list. If the size of the - * list is even, there is no middle value. So the median is the mean of the two - * middle value. - * For example, - * - * [2,3,4], the median is 3 - * - * [2,3], the median is (2 + 3) / 2 = 2.5 - * - * Design a data structure that supports the following two operations: - * - * - * void addNum(int num) - Add a integer number from the data stream to the data - * structure. - * double findMedian() - Return the median of all elements so far. - * - * - * - * - * Example: - * - * - * addNum(1) - * addNum(2) - * findMedian() -> 1.5 - * addNum(3) - * findMedian() -> 2 - * - * - * - * - * Follow up: - * - * - * If all integer numbers from the stream are between 0 and 100, how would you - * optimize it? - * If 99% of all integer numbers from the stream are between 0 and 100, how - * would you optimize it? - * - * - */ -/** - * initialize your data structure here. - */ var MedianFinder = function() { this.maxHeap = new PriorityQueue((a, b) => a - b); this.minHeap = new PriorityQueue((a, b) => b - a); diff --git a/problems/3.longest-substring-without-repeating-characters.md b/problems/3.longest-substring-without-repeating-characters.md index d8c23f160..f48aa8e52 100644 --- a/problems/3.longest-substring-without-repeating-characters.md +++ b/problems/3.longest-substring-without-repeating-characters.md @@ -89,8 +89,6 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 - -大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 - -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubtysc0j30p00dwt9t.jpg) +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/30.substring-with-concatenation-of-all-words.md b/problems/30.substring-with-concatenation-of-all-words.md index 842a6cae4..8e6beab24 100644 --- a/problems/30.substring-with-concatenation-of-all-words.md +++ b/problems/30.substring-with-concatenation-of-all-words.md @@ -86,3 +86,14 @@ class Solution: i += 1 return res ``` + +**复杂度分析** + +其中 N 为 words 中的总字符数。 + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/32.longest-valid-parentheses.md b/problems/32.longest-valid-parentheses.md index 2f5cf75d0..6392fcc6a 100644 --- a/problems/32.longest-valid-parentheses.md +++ b/problems/32.longest-valid-parentheses.md @@ -273,6 +273,11 @@ class Solution: return mlen ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ### 关键点解析 1. 第 3 点特征, 需要检查的字符是 s[i-1]和 s[i-2-dp[i-1]], 根据定义可知: i-1 >= dp[i-1], 但是 i-2 不一定大于 dp[i-1], 因此, 需要检查越界; @@ -286,3 +291,8 @@ class Solution: 1. 如果判断的不仅仅只有'('和')', 还有'[', ']', '{'和'}', 该怎么办? 2. 如果输出的不是长度, 而是任意一个最长有效括号对的字符串, 该怎么办? + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/328.odd-even-linked-list.md b/problems/328.odd-even-linked-list.md index c9f765bb3..a0a999cab 100644 --- a/problems/328.odd-even-linked-list.md +++ b/problems/328.odd-even-linked-list.md @@ -132,3 +132,12 @@ public: } }; ``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/33.search-in-rotated-sorted-array.md b/problems/33.search-in-rotated-sorted-array.md index 7ed384aa1..fc60dec72 100644 --- a/problems/33.search-in-rotated-sorted-array.md +++ b/problems/33.search-in-rotated-sorted-array.md @@ -80,7 +80,7 @@ nums 肯定会在某个点上旋转 ## 关键点解析 -- 二分法 +- [二分法](../91/binary-search.md) - 找出有序区间,然后根据target是否在有序区间舍弃一半元素 ## 代码 @@ -169,3 +169,12 @@ class Solution: return left if nums[left] == target else -1 ``` + +**复杂度分析** + +- 时间复杂度:$O(logN)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/334.increasing-triplet-subsequence.md b/problems/334.increasing-triplet-subsequence.md index 7d814b5cf..6da292e9a 100644 --- a/problems/334.increasing-triplet-subsequence.md +++ b/problems/334.increasing-triplet-subsequence.md @@ -114,3 +114,12 @@ var increasingTriplet = function(nums) { return false; }; ``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/335.self-crossing.md b/problems/335.self-crossing.md index 290afba41..4ab65ea71 100644 --- a/problems/335.self-crossing.md +++ b/problems/335.self-crossing.md @@ -123,3 +123,14 @@ class Solution: return True return False ``` + +**复杂度分析** + +其中 N 为数组长度。 + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/337.house-robber-iii.md b/problems/337.house-robber-iii.md index 1fc289ddf..726b825f0 100644 --- a/problems/337.house-robber-iii.md +++ b/problems/337.house-robber-iii.md @@ -196,8 +196,7 @@ class Solution: - [198.house-robber](https://github.com/azl397985856/leetcode/blob/master/problems/198.house-robber.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludu08hcj30p00dwt9t.jpg) diff --git a/problems/342.power-of-four.md b/problems/342.power-of-four.md index f348ef421..52a0e24af 100644 --- a/problems/342.power-of-four.md +++ b/problems/342.power-of-four.md @@ -144,7 +144,7 @@ class Solution: - 时间复杂度:$O(1)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/349.intersection-of-two-arrays.md b/problems/349.intersection-of-two-arrays.md index bf5f91b23..3dce1e855 100644 --- a/problems/349.intersection-of-two-arrays.md +++ b/problems/349.intersection-of-two-arrays.md @@ -103,7 +103,7 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/371.sum-of-two-integers.md b/problems/371.sum-of-two-integers.md index 8413bb82f..f41bdda9a 100644 --- a/problems/371.sum-of-two-integers.md +++ b/problems/371.sum-of-two-integers.md @@ -135,7 +135,7 @@ class Solution: > 由于题目数据规模不会变化,因此其实复杂度分析是没有意义的。 -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/378.kth-smallest-element-in-a-sorted-matrix.md b/problems/378.kth-smallest-element-in-a-sorted-matrix.md index 44da543cf..3cd01bb4c 100644 --- a/problems/378.kth-smallest-element-in-a-sorted-matrix.md +++ b/problems/378.kth-smallest-element-in-a-sorted-matrix.md @@ -165,6 +165,15 @@ var kthSmallest = function(matrix, k) { }; ``` +**复杂度分析** + +- 时间复杂度:二分查找进行次数为 $O(log(r-l))$,每次操作时间复杂度为 O(n),因此总的时间复杂度为 $O(nlog(r-l))$。 +- 空间复杂度:$O(1)$。 + ## 相关题目 - [240.search-a-2-d-matrix-ii](./240.search-a-2-d-matrix-ii.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/4.median-of-two-sorted-arrays.md b/problems/4.median-of-two-sorted-arrays.md index 0303609be..3f3b2c1cf 100644 --- a/problems/4.median-of-two-sorted-arrays.md +++ b/problems/4.median-of-two-sorted-arrays.md @@ -283,8 +283,8 @@ class MedianSortedTwoArrayBinarySearch { - 时间复杂度:$O(log(min(m, n)))$ - 空间复杂度:$O(log(min(m, n)))$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 - -大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzedtodj31bi0hcq5s.jpg) diff --git a/problems/416.partition-equal-subset-sum.md b/problems/416.partition-equal-subset-sum.md index 279deaad1..098e5cac2 100644 --- a/problems/416.partition-equal-subset-sum.md +++ b/problems/416.partition-equal-subset-sum.md @@ -294,6 +294,16 @@ var change = function (amount, coins) { }; ``` +**复杂度分析** + +- 时间复杂度:$O(amount * len(coins))$ +- 空间复杂度:$O(amount)$ + ### 参考 [背包九讲](https://raw.githubusercontent.com/tianyicui/pack/master/V2.pdf) + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/42.trapping-rain-water.md b/problems/42.trapping-rain-water.md index c11180ee6..2ab40f2ec 100755 --- a/problems/42.trapping-rain-water.md +++ b/problems/42.trapping-rain-water.md @@ -157,7 +157,7 @@ int trap(vector& heights) ### 思路 -上面代码比较好理解,但是需要额外的 \${N} 的空间。从上面解法可以看出,我们实际上只关心左右两侧较小的那一个,并不需要两者都计算出来。具体来说: +上面代码比较好理解,但是需要额外的 N 的空间。从上面解法可以看出,我们实际上只关心左右两侧较小的那一个,并不需要两者都计算出来。具体来说: - 如果 l[i + 1] < r[i] 那么 最终积水的高度由 i 的左侧最大值决定。 - 如果 l[i + 1] >= r[i] 那么 最终积水的高度由 i 的右侧最大值决定。 @@ -241,7 +241,7 @@ public: - [84.largest-rectangle-in-histogram](https://github.com/azl397985856/leetcode/blob/master/problems/84.largest-rectangle-in-histogram.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/437.path-sum-iii.md b/problems/437.path-sum-iii.md index eb3ef533b..0bb91c474 100644 --- a/problems/437.path-sum-iii.md +++ b/problems/437.path-sum-iii.md @@ -173,7 +173,7 @@ var pathSum = function(root, sum) { - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/445.add-two-numbers-ii.md b/problems/445.add-two-numbers-ii.md index f98c09987..0e403b2bf 100644 --- a/problems/445.add-two-numbers-ii.md +++ b/problems/445.add-two-numbers-ii.md @@ -273,3 +273,14 @@ class Solution: return result.next ``` + +**复杂度分析** + +其中 M 和 N 分别为两个链表的长度。 + +- 时间复杂度:$O(M + N)$ +- 空间复杂度:$O(M + N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/454.4-sum-ii.md b/problems/454.4-sum-ii.md index 09d3f3fb0..ede914aea 100644 --- a/problems/454.4-sum-ii.md +++ b/problems/454.4-sum-ii.md @@ -112,3 +112,12 @@ class Solution: res += mapper.get(-1 * (i + j), 0) return res ``` + + **复杂度分析** + +- 时间复杂度:$O(N^2)$ +- 空间复杂度:$O(N^2)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/455.AssignCookies.md b/problems/455.AssignCookies.md index 4f41d84d0..fb66e57a1 100644 --- a/problems/455.AssignCookies.md +++ b/problems/455.AssignCookies.md @@ -94,7 +94,7 @@ const findContentChildren = function (g, s) { - 时间复杂度:由于使用了排序,因此时间复杂度为 O(NlogN) - 空间复杂度:O(1) -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/472.concatenated-words.md b/problems/472.concatenated-words.md index 5082446b6..d61048063 100644 --- a/problems/472.concatenated-words.md +++ b/problems/472.concatenated-words.md @@ -129,3 +129,8 @@ class Solution: - [0212.word-search-ii](https://github.com/azl397985856/leetcode/blob/b0b69f8f11dace3a9040b54532105d42e88e6599/problems/212.word-search-ii.md) - [0820.short-encoding-of-words](https://github.com/azl397985856/leetcode/blob/master/problems/820.short-encoding-of-words.md) - [1032.stream-of-characters](https://github.com/azl397985856/leetcode/blob/master/problems/1032.stream-of-characters.md) + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/48.rotate-image.md b/problems/48.rotate-image.md index 118488768..a25cfd8c2 100644 --- a/problems/48.rotate-image.md +++ b/problems/48.rotate-image.md @@ -160,3 +160,12 @@ class Solution: """ matrix[:] = map(list, zip(*matrix[::-1])) ``` + +**复杂度分析** + +- 时间复杂度:$O(M * N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/49.group-anagrams.md b/problems/49.group-anagrams.md index f3102e051..f43eebb9d 100644 --- a/problems/49.group-anagrams.md +++ b/problems/49.group-anagrams.md @@ -132,3 +132,14 @@ class Solution: str_dict[tuple(s_key)].append(s) return str_dict.values() ``` + +**复杂度分析** + +其中 N 为 strs 的长度, M 为 strs 中字符串的平均长度。 + +- 时间复杂度:$O(N * M)$ +- 空间复杂度:$O(N * M)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/493.reverse-pairs.md b/problems/493.reverse-pairs.md index b0fd1a837..55a6967b6 100644 --- a/problems/493.reverse-pairs.md +++ b/problems/493.reverse-pairs.md @@ -115,6 +115,15 @@ class Solution(object): ``` +**复杂度分析** + +- 时间复杂度:$O(NlogN)$ +- 空间复杂度:$O(logN)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + 对于具体的排序过程我们偷懒直接使用了语言内置的方法 sorted,这在很多时候是有用的,即使你是参加面试,这种方式通常也是允许的。省略非核心的考点,可以使得问题更加聚焦,这是一种解决问题的思路,在工作中也很有用。 ## 关键点解析 diff --git a/problems/494.target-sum.md b/problems/494.target-sum.md index 307207159..35fae4e43 100644 --- a/problems/494.target-sum.md +++ b/problems/494.target-sum.md @@ -110,6 +110,15 @@ var findTargetSumWays = function(nums, S) { }; ``` +**复杂度分析** + +- 时间复杂度:$O(N * target)$ +- 空间复杂度:$O(target)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + ## 扩展 如果这道题目并没有限定所有的元素以及 target 都是正数怎么办? diff --git a/problems/5.longest-palindromic-substring.md b/problems/5.longest-palindromic-substring.md index f8f8ec028..f775c4daa 100644 --- a/problems/5.longest-palindromic-substring.md +++ b/problems/5.longest-palindromic-substring.md @@ -135,12 +135,12 @@ var longestPalindrome = function (s) { - 时间复杂度:$O(N^2)$ - 空间复杂度:$O(N^2)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 - -大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 - -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluapqlq5j31bi0hcq5s.jpg) - ## 相关题目 - [516.longest-palindromic-subsequence](./516.longest-palindromic-subsequence.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + +![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluapqlq5j31bi0hcq5s.jpg) diff --git a/problems/50.pow-x-n.md b/problems/50.pow-x-n.md index e791c8e78..310ddeebf 100644 --- a/problems/50.pow-x-n.md +++ b/problems/50.pow-x-n.md @@ -186,6 +186,11 @@ class Solution: return res ``` +**复杂度分析** + +- 时间复杂度:$O(logN)$ +- 空间复杂度:$O(1)$ + ## 关键点解析 - 超时分析 @@ -199,3 +204,7 @@ class Solution: - [458.可怜的小猪](https://leetcode-cn.com/problems/poor-pigs/description/) ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwu19yqj30wn0u0abv.jpg) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/516.longest-palindromic-subsequence.md b/problems/516.longest-palindromic-subsequence.md index 9fbbaa2c6..a4c465ee0 100644 --- a/problems/516.longest-palindromic-subsequence.md +++ b/problems/516.longest-palindromic-subsequence.md @@ -116,6 +116,16 @@ var longestPalindromeSubseq = function(s) { }; ``` +**复杂度分析** + +- 时间复杂度:$O(N^2)$ +- 空间复杂度:$O(N^2)$ + ## 相关题目 - [5.longest-palindromic-substring](./5.longest-palindromic-substring.md) + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/518.coin-change-2.md b/problems/518.coin-change-2.md index f4b446573..c4ae4de51 100644 --- a/problems/518.coin-change-2.md +++ b/problems/518.coin-change-2.md @@ -179,6 +179,11 @@ class Solution: return dp[-1] ``` +**复杂度分析** + +- 时间复杂度:$O(amount)$ +- 空间复杂度:$O(amount * len(coins))$ + ## 扩展 这是一道很简单描述的题目, 因此很多时候会被用到大公司的电面中。 @@ -206,3 +211,13 @@ class Solution: dp[i][j] = dp[i][j - 1] return dp[-1][-1] ``` + +**复杂度分析** + +- 时间复杂度:$O(amount * len(coins))$ +- 空间复杂度:$O(amount * len(coins))$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 + +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/53.maximum-sum-subarray-cn.md b/problems/53.maximum-sum-subarray-cn.md index 0c13b78d3..f4bb2763a 100644 --- a/problems/53.maximum-sum-subarray-cn.md +++ b/problems/53.maximum-sum-subarray-cn.md @@ -416,8 +416,7 @@ function LSS(list) { - [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) - [Longest Turbulent Subarray](https://leetcode.com/problems/longest-turbulent-subarray/) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/55.jump-game.md b/problems/55.jump-game.md index 49d953fcf..ac1c418a8 100644 --- a/problems/55.jump-game.md +++ b/problems/55.jump-game.md @@ -37,15 +37,16 @@ https://leetcode-cn.com/problems/jump-game/ ## 思路 -这道题目是一道典型的`贪心`类型题目。思路就是用一个变量记录当前能够到达的最大的索引,我们逐个遍历数组中的元素去更新这个索引。变量完成判断这个索引是否大于数组下表即可。 +这道题目是一道典型的`贪心`类型题目。思路就是用一个变量记录当前能够到达的最大的索引,并逐个遍历数组中的元素去更新这个索引,遍历完成判断这个索引是否大于`数组长度 - 1`即可。 ## 关键点解析 -- 建模 (记录和更新当前位置能够到达的最大的索引即可) +- 记录和更新当前位置能够到达的最大的索引 ## 代码 - 语言支持: Javascript,C++,Java,Python3 + Javascript Code: ```js /** @@ -136,6 +137,6 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(1)$ -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 - -大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/575.distribute-candies.md b/problems/575.distribute-candies.md index 2271e4a8e..75c363b1d 100644 --- a/problems/575.distribute-candies.md +++ b/problems/575.distribute-candies.md @@ -84,7 +84,7 @@ class Solution: - 时间复杂度:$O(N)$ - 空间复杂度:$O(N)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/60.permutation-sequence.md b/problems/60.permutation-sequence.md index 2a539e3fa..efa423f7d 100644 --- a/problems/60.permutation-sequence.md +++ b/problems/60.permutation-sequence.md @@ -125,3 +125,13 @@ class Solution: n -= 1 return res ``` + + +**复杂度分析** + +- 时间复杂度:$O(N^2)$ +- 空间复杂度:$O(N)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) \ No newline at end of file diff --git a/problems/609.find-duplicate-file-in-system.md b/problems/609.find-duplicate-file-in-system.md index f6b0e3e13..a978c8f91 100644 --- a/problems/609.find-duplicate-file-in-system.md +++ b/problems/609.find-duplicate-file-in-system.md @@ -59,87 +59,6 @@ https://leetcode-cn.com/problems/find-duplicate-file-in-system/ ## 代码 ```js - - -/* - * @lc app=leetcode id=609 lang=javascript - * - * [609] Find Duplicate File in System - * - * https://leetcode.com/problems/find-duplicate-file-in-system/description/ - * - * algorithms - * Medium (54.21%) - * Total Accepted: 24.1K - * Total Submissions: 44.2K - * Testcase Example: '["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"]' - * - * Given a list of directory info including directory path, and all the files - * with contents in this directory, you need to find out all the groups of - * duplicate files in the file system in terms of their paths. - * - * A group of duplicate files consists of at least two files that have exactly - * the same content. - * - * A single directory info string in the input list has the following format: - * - * "root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... - * fn.txt(fn_content)" - * - * It means there are n files (f1.txt, f2.txt ... fn.txt with content - * f1_content, f2_content ... fn_content, respectively) in directory - * root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the - * directory is just the root directory. - * - * The output is a list of group of duplicate file paths. For each group, it - * contains all the file paths of the files that have the same content. A file - * path is a string that has the following format: - * - * "directory_path/file_name.txt" - * - * Example 1: - * - * - * Input: - * ["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d - * 4.txt(efgh)", "root 4.txt(efgh)"] - * Output: - * - * [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]] - * - * - * - * - * Note: - * - * - * No order is required for the final output. - * You may assume the directory name, file name and file content only has - * letters and digits, and the length of file content is in the range of - * [1,50]. - * The number of files given is in the range of [1,20000]. - * You may assume no files or directories share the same name in the same - * directory. - * You may assume each given directory info represents a unique directory. - * Directory path and file info are separated by a single blank space. - * - * - * - * Follow-up beyond contest: - * - * - * Imagine you are given a real file system, how will you search files? DFS or - * BFS? - * If the file content is very large (GB level), how will you modify your - * solution? - * If you can only read the file by 1kb each time, how will you modify your - * solution? - * What is the time complexity of your modified solution? What is the most - * time-consuming part and memory consuming part of it? How to optimize? - * How to make sure the duplicated files you find are not false positive? - * - * - */ /** * @param {string[]} paths * @return {string[][]} @@ -163,16 +82,3 @@ var findDuplicate = function(paths) { return Object.values(hashmap).filter(q => q.length >= 2); }; ``` - -## 扩展 -leetcode官方给的扩展我觉得就很有意思,虽然很`老套`, 这里还是列一下好了,大家可以作为思考题来思考一下。 - -1. Imagine you are given a real file system, how will you search files? DFS or BFS? - -2. If the file content is very large (GB level), how will you modify your solution? - -3. If you can only read the file by 1kb each time, how will you modify your solution? - -4. What is the time complexity of your modified solution? What is the most time-consuming part and memory consuming part of it? How to optimize? - -5. How to make sure the duplicated files you find are not false positive? diff --git a/problems/611.valid-triangle-number.md b/problems/611.valid-triangle-number.md index 58b95cc6d..c1291bb99 100644 --- a/problems/611.valid-triangle-number.md +++ b/problems/611.valid-triangle-number.md @@ -152,7 +152,7 @@ class Solution: - 时间复杂度:$O(N ^ 2)$ - 空间复杂度:取决于排序算法 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/718.maximum-length-of-repeated-subarray.md b/problems/718.maximum-length-of-repeated-subarray.md index dfe06b8c5..2f33e82aa 100644 --- a/problems/718.maximum-length-of-repeated-subarray.md +++ b/problems/718.maximum-length-of-repeated-subarray.md @@ -81,7 +81,7 @@ class Solution: 二分查找也是可以的,不过并不容易想到,大家可以试试。 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/73.set-matrix-zeroes.md b/problems/73.set-matrix-zeroes.md index 7f096bfef..800ca66d7 100644 --- a/problems/73.set-matrix-zeroes.md +++ b/problems/73.set-matrix-zeroes.md @@ -262,6 +262,15 @@ class Solution: matrix[r][c] = 0 ``` +**复杂度分析** + +- 时间复杂度:$O(M * N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) + ## 扩展 为什么选择第一行第一列,选择其他行和列可以么?为什么? diff --git a/problems/75.sort-colors.md b/problems/75.sort-colors.md index 28f06f5e1..ea19eb2a9 100644 --- a/problems/75.sort-colors.md +++ b/problems/75.sort-colors.md @@ -88,3 +88,12 @@ class Solution: else: cur += 1 ``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/785.is-graph-bipartite.md b/problems/785.is-graph-bipartite.md index d2086dc99..6a08c37bc 100644 --- a/problems/785.is-graph-bipartite.md +++ b/problems/785.is-graph-bipartite.md @@ -109,6 +109,6 @@ class Solution: - [886. 可能的二分法](./886.possible-bipartition.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/80.remove-duplicates-from-sorted-array-ii.md b/problems/80.remove-duplicates-from-sorted-array-ii.md index e975739da..09bf1ab8e 100644 --- a/problems/80.remove-duplicates-from-sorted-array-ii.md +++ b/problems/80.remove-duplicates-from-sorted-array-ii.md @@ -101,6 +101,11 @@ class Solution: return i ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + 基于这套代码,你可以轻易地实现 k 为任意正整数的算法。 ## 相关题目 @@ -114,3 +119,9 @@ class Solution: - 83. 删除排序链表中的重复元素 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0vrcvlj318c0se0wm.jpg) + + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/86.partition-list.md b/problems/86.partition-list.md index 9529072cc..6837f91bf 100644 --- a/problems/86.partition-list.md +++ b/problems/86.partition-list.md @@ -55,40 +55,6 @@ https://leetcode-cn.com/problems/partition-list/ - 语言支持: Javascript,Python3 ```js -/* - * @lc app=leetcode id=86 lang=javascript - * - * [86] Partition List - * - * https://leetcode.com/problems/partition-list/description/ - * - * algorithms - * Medium (36.41%) - * Total Accepted: 155.1K - * Total Submissions: 425.1K - * Testcase Example: '[1,4,3,2,5,2]\n3' - * - * Given a linked list and a value x, partition it such that all nodes less - * than x come before nodes greater than or equal to x. - * - * You should preserve the original relative order of the nodes in each of the - * two partitions. - * - * Example: - * - * - * Input: head = 1->4->3->2->5->2, x = 3 - * Output: 1->2->2->4->3->5 - * - * - */ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ /** * @param {ListNode} head * @param {number} x @@ -162,3 +128,12 @@ class Solution: return first_node.next ``` + +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(1)$ + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/874.walking-robot-simulation.md b/problems/874.walking-robot-simulation.md index 32d4514a8..c5ee18e3c 100644 --- a/problems/874.walking-robot-simulation.md +++ b/problems/874.walking-robot-simulation.md @@ -129,7 +129,7 @@ class Solution: - 时间复杂度:$O(N * M)$, 其中 N 为 commands 的长度, M 为 commands 数组的平均值。 - 空间复杂度:$O(obstacles)$ -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。 +更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经37K star啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/875.koko-eating-bananas.md b/problems/875.koko-eating-bananas.md index 68aa950d4..31b6f243a 100644 --- a/problems/875.koko-eating-bananas.md +++ b/problems/875.koko-eating-bananas.md @@ -1,7 +1,9 @@ ## 题目地址(875. 爱吃香蕉的珂珂) + https://leetcode-cn.com/problems/koko-eating-bananas/description/ ## 题目描述 + ``` 珂珂喜欢吃香蕉。这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 H 小时后回来。 @@ -45,7 +47,8 @@ piles.length <= H <= 10^9 - 字节 ## 思路 -符合直觉的做法是,选择最大的堆的香蕉数,然后试一下能不能行,如果不行则直接返回上次计算的结果,如果行,我们减少1个香蕉,试试行不行,依次类推。计算出刚好不行的即可。这种解法的时间复杂度比较高,为 $O(N * M)$,其中 N 为 piles 长度, M 为 Piles 中最大的数。。 + +符合直觉的做法是,选择最大的堆的香蕉数,然后试一下能不能行,如果不行则直接返回上次计算的结果,如果行,我们减少 1 个香蕉,试试行不行,依次类推。计算出刚好不行的即可。这种解法的时间复杂度比较高,为 $O(N * M)$,其中 N 为 piles 长度, M 为 Piles 中最大的数。。 这道题如果能看出来是二分法解决,那么其实很简单。为什么它是二分问题呢?我这里画了个图,我相信你看了就明白了。 @@ -57,13 +60,12 @@ piles.length <= H <= 10^9 - 二分查找 - ## 代码 - 代码支持:Python,JavaScript - - Python Code: - +代码支持:Python,JavaScript + +Python Code: + ```py class Solution: def canEatAllBananas(self, piles, H, K): @@ -73,7 +75,7 @@ class Solution: return t <= H def minEatingSpeed(self, piles: List[int], H: int) -> int: l, r = 1, max(piles) - # [l, r) , 左闭右开的好处是如果能找到,那么返回 l 和 r 都是一样的,因为最终 l 等于 r。 + # [l, r) , 左闭右开的好处是如果能找到,那么返回 l 和 r 都是一样的,因为最终 l 等于 r。 while l < r: mid = (l + r) >> 1 if self.canEatAllBananas(piles, H, mid): @@ -83,43 +85,42 @@ class Solution: return l ``` - - - JavaScript Code: -```js +JavaScript Code: +```js function canEatAllBananas(piles, H, mid) { - let h = 0; - for(let pile of piles) { - h += Math.ceil(pile / mid); - } + let h = 0; + for (let pile of piles) { + h += Math.ceil(pile / mid); + } - return h <= H; - } + return h <= H; +} /** * @param {number[]} piles * @param {number} H * @return {number} */ -var minEatingSpeed = function(piles, H) { - let lo = 1, +var minEatingSpeed = function (piles, H) { + let lo = 1, hi = Math.max(...piles); - // [l, r) , 左闭右开的好处是如果能找到,那么返回 l 和 r 都是一样的,因为最终 l 等于 r。 - while(lo <= hi) { - let mid = lo + ((hi - lo) >> 1); - if (canEatAllBananas(piles, H, mid)) { - hi = mid - 1; - } else { - lo = mid + 1; - } + // [l, r) , 左闭右开的好处是如果能找到,那么返回 l 和 r 都是一样的,因为最终 l 等于 r。 + while (lo <= hi) { + let mid = lo + ((hi - lo) >> 1); + if (canEatAllBananas(piles, H, mid)) { + hi = mid - 1; + } else { + lo = mid + 1; } + } - return lo; // 不能选择hi + return lo; // 不能选择hi }; ``` **复杂度分析** + - 时间复杂度:$O(max(N, N * logM))$,其中 N 为 piles 长度, M 为 Piles 中最大的数。 - 空间复杂度:$O(1)$ @@ -127,19 +128,18 @@ var minEatingSpeed = function(piles, H) { 分享几个常用的的二分法模板。 - ### 查找一个数 ```java public int binarySearch(int[] nums, int target) { // 左右都闭合的区间 [l, r] - int left = 0; + int left = 0; int right = nums.length - 1; while(left <= right) { int mid = left + (right - left) / 2; if(nums[mid] == target) - return mid; + return mid; else if (nums[mid] < target) // 搜索区间变为 [mid+1, right] left = mid + 1; @@ -207,10 +207,8 @@ public int binarySearchRight(int[] nums, int target) { > 如果题目重点不是二分,也就是说二分只是众多步骤中的一步,大家也可以直接调用语言的 API,比如 Python 的 bisect 模块。 -更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 - ![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4sl5v4j30p00dwt9t.jpg) - diff --git a/problems/877.stone-game.md b/problems/877.stone-game.md index 2eacf4e2e..e43d3000b 100644 --- a/problems/877.stone-game.md +++ b/problems/877.stone-game.md @@ -67,8 +67,6 @@ sum(piles) 是奇数。 - 可以从数学的角度去分析 -> ......(😅) - ## 代码 ```js diff --git a/problems/886.possible-bipartition.md b/problems/886.possible-bipartition.md index c726e66c4..44196b43d 100644 --- a/problems/886.possible-bipartition.md +++ b/problems/886.possible-bipartition.md @@ -146,6 +146,6 @@ class Solution: - [785. 判断二分图](785.is-graph-bipartite.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/895.maximum-frequency-stack.md b/problems/895.maximum-frequency-stack.md index 67521e180..0d6145fc9 100644 --- a/problems/895.maximum-frequency-stack.md +++ b/problems/895.maximum-frequency-stack.md @@ -112,7 +112,8 @@ class FreqStack: # param_2 = obj.pop() ``` -***复杂度分析*** +**复杂度分析** + - 时间复杂度:push 和 pop 平均时间复杂度是 $O(1)$ - 空间复杂度:$O(N)$,其中N为数字的总数。 diff --git a/problems/91.decode-ways.md b/problems/91.decode-ways.md index 18dbcb7c6..6fde5b247 100644 --- a/problems/91.decode-ways.md +++ b/problems/91.decode-ways.md @@ -85,49 +85,6 @@ s 只包含数字,并且可以包含前导零。 ## 代码 ```js -/* - * @lc app=leetcode id=91 lang=javascript - * - * [91] Decode Ways - * - * https://leetcode.com/problems/decode-ways/description/ - * - * algorithms - * Medium (21.93%) - * Total Accepted: 254.4K - * Total Submissions: 1.1M - * Testcase Example: '"12"' - * - * A message containing letters from A-Z is being encoded to numbers using the - * following mapping: - * - * - * 'A' -> 1 - * 'B' -> 2 - * ... - * 'Z' -> 26 - * - * - * Given a non-empty string containing only digits, determine the total number - * of ways to decode it. - * - * Example 1: - * - * - * Input: "12" - * Output: 2 - * Explanation: It could be decoded as "AB" (1 2) or "L" (12). - * - * - * Example 2: - * - * - * Input: "226" - * Output: 3 - * Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 - * 6). - * - */ /** * @param {string} s * @return {number} @@ -156,6 +113,15 @@ var numDecodings = function (s) { }; ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 扩展 如果编码的范围不再是 1-26,而是三位的话怎么办? + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/92.reverse-linked-list-ii.md b/problems/92.reverse-linked-list-ii.md index f0db86a7e..a4ac6aa83 100644 --- a/problems/92.reverse-linked-list-ii.md +++ b/problems/92.reverse-linked-list-ii.md @@ -255,7 +255,7 @@ class Solution: - [25.reverse-nodes-in-k-groups](./25.reverse-nodes-in-k-groups-cn.md) - [206.reverse-linked-list](./206.reverse-linked-list.md) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/problems/94.binary-tree-inorder-traversal.md b/problems/94.binary-tree-inorder-traversal.md index 7895b97ed..d9d670466 100644 --- a/problems/94.binary-tree-inorder-traversal.md +++ b/problems/94.binary-tree-inorder-traversal.md @@ -252,3 +252,8 @@ class Solution { ## 相关专题 - [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md) + + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) \ No newline at end of file diff --git a/problems/98.validate-binary-search-tree.md b/problems/98.validate-binary-search-tree.md index 6f8d95e70..4c519c6ae 100644 --- a/problems/98.validate-binary-search-tree.md +++ b/problems/98.validate-binary-search-tree.md @@ -352,6 +352,15 @@ function valid(root, min = -Infinity, max = Infinity) { } ``` +**复杂度分析** + +- 时间复杂度:$O(N)$ +- 空间复杂度:$O(N)$ + ## 相关题目 [230.kth-smallest-element-in-a-bst](./230.kth-smallest-element-in-a-bst.md) + +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 +![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/selected/LCS.md b/selected/LCS.md index 4214cad90..c7c492e43 100644 --- a/selected/LCS.md +++ b/selected/LCS.md @@ -255,6 +255,6 @@ class Solution: **只有熟练掌握基础的数据结构与算法,才能对复杂问题迎刃有余。** 基础算法,把它彻底搞懂,再去面对出题人的各种换皮就不怕了。相反,如果你不去思考题目背后的逻辑,就会刷地很痛苦。题目稍微一变化你就不会了,这也是为什么很多人说**刷了很多题,但是碰到新的题目还是不会做**的原因之一。关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/selected/LIS.md b/selected/LIS.md index 345bca36a..ce7dd1686 100644 --- a/selected/LIS.md +++ b/selected/LIS.md @@ -300,6 +300,6 @@ class Solution: 大家把我讲的思路搞懂,这几个题一写,还怕碰到类似的题不会么?**只有熟练掌握基础的数据结构与算法,才能对复杂问题迎刃有余。** 最长上升子序列就是一个非常经典的基础算法,把它彻底搞懂,再去面对出题人的各种换皮就不怕了。相反,如果你不去思考题目背后的逻辑,就会刷地很痛苦。题目稍微一变化你就不会了,这也是为什么很多人说**刷了很多题,但是碰到新的题目还是不会做**的原因之一。关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/selected/README.md b/selected/README.md index f0d12f7dd..77bfe1cf7 100644 --- a/selected/README.md +++ b/selected/README.md @@ -1,14 +1,18 @@ - # 精选题解 - +# 精选题解 + +这里是我以前写的题解。 这里的题解一般都是多个题目,而不是针对某一具体题目的。熟悉我的朋友应该知道了, 这是我所说的**第二阶段** 。 + +第一阶段按照 tag 去刷, 第二阶段则要一题多解,多题同解,挖掘题目背后的东西。而这个系列大多数就是做了这个事情。其他不是**一题多解,多题同解**的,基本就是**大厂真题解析**。 + - [《日程安排》专题](./schedule-topic.md) - [《构造二叉树》专题](./construct-binary-tree.md) - [字典序列删除](./a-deleted.md) -- [百度的算法面试题 - 祖玛游戏](./zuma-game.md) -- [西法带你学算法】一次搞定前缀和](./atMostK.md) +- [百度的算法面试题 - 祖玛游戏](./zuma-game.md) +- [西法的刷题秘籍】一次搞定前缀和](./atMostK.md) - [字节跳动的算法面试题是什么难度?](./byte-dance-algo-ex.md) - [字节跳动的算法面试题是什么难度?(第二弹)](./byte-dance-algo-ex-2017.md) -- [《我是你的妈妈呀》 - 第一期](./mother-01.md) +- [《我是你的妈妈呀》 - 第一期](./mother-01.md) - [一文带你看懂二叉树的序列化](./serialize.md) - [穿上衣服我就不认识你了?来聊聊最长上升子序列](./LIS.md) -- [你的衣服我扒了 - 《最长公共子序列》](./LCS.md) -- [一文看懂《最大子序列和问题》](./LSS.md) \ No newline at end of file +- [你的衣服我扒了 - 《最长公共子序列》](./LCS.md) +- [一文看懂《最大子序列和问题》](./LSS.md) diff --git a/selected/a-deleted.md b/selected/a-deleted.md index 20df2e5b1..97c67c73b 100644 --- a/selected/a-deleted.md +++ b/selected/a-deleted.md @@ -378,7 +378,7 @@ class Solution: - [【简单易懂】归并排序(Python)](https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/solution/jian-dan-yi-dong-gui-bing-pai-xu-python-by-azl3979/) - [一文看懂《最大子序列和问题》](https://lucifer.ren/blog/2019/12/11/LSS/) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 diff --git a/selected/atMostK.md b/selected/atMostK.md index 2ea018907..26081919e 100644 --- a/selected/atMostK.md +++ b/selected/atMostK.md @@ -1,4 +1,4 @@ -# 【西法带你学算法】一次搞定前缀和 +# 【西法的刷题秘籍】一次搞定前缀和 我花了几天时间,从力扣中精选了五道相同思想的题目,来帮助大家解套,如果觉得文章对你有用,记得点赞分享,让我看到你的认可,有动力继续做下去。 diff --git a/selected/construct-binary-tree.md b/selected/construct-binary-tree.md index 914b5d509..90b89e31b 100644 --- a/selected/construct-binary-tree.md +++ b/selected/construct-binary-tree.md @@ -252,7 +252,7 @@ node.right = self.constructFromPrePost(pre[i + 2:], post[i + 1:-1]) ## 关注我 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 diff --git a/selected/mother-01.md b/selected/mother-01.md index 1a782f372..05dcea61e 100644 --- a/selected/mother-01.md +++ b/selected/mother-01.md @@ -403,8 +403,7 @@ mergeKLists 执行了 logk 次,每次都执行一次 mergeTwoLists,mergeTwoL 本期给大家介绍了八道母题, 大家可以在之后的刷题过程中尝试使用母题来套模板。之后会给大家带来更多的母题。 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 35K star 啦。 - -关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/selected/schedule-topic.md b/selected/schedule-topic.md index c4b5c8204..f2d2e7d83 100644 --- a/selected/schedule-topic.md +++ b/selected/schedule-topic.md @@ -155,7 +155,7 @@ class MyCalendar: ### 思路 -和上面思路类似,只不过我们每次都对 calendars 进行排序,那么我们可以通过二分查找日程安排的情况来检查新日常安排是否可以预订。如果每次插入之前都进行一次排序,那么时间复杂度会很高。如图,我们的[s1,e1], [s2,e2], [s3,e3] 是按照时间顺序排好的日程安排。我们现在要插入[s,e],我们使用二分查找,找到要插入的位置,然后和插入位置的课程进行一次比对即可,这部分的时间复杂度是 O(logN)\$。 +和上面思路类似,只不过我们每次都对 calendars 进行排序,那么我们可以通过二分查找日程安排的情况来检查新日常安排是否可以预订。如果每次插入之前都进行一次排序,那么时间复杂度会很高。如图,我们的[s1,e1], [s2,e2], [s3,e3] 是按照时间顺序排好的日程安排。我们现在要插入[s,e],我们使用二分查找,找到要插入的位置,然后和插入位置的课程进行一次比对即可,这部分的时间复杂度是 $O(logN)$。 ![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbj28k6v4gj21100c2754.jpg) diff --git a/selected/serialize.md b/selected/serialize.md index 4ef9b764b..93639c4c9 100644 --- a/selected/serialize.md +++ b/selected/serialize.md @@ -298,6 +298,6 @@ Python 代码: (Like This) -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/thinkings/README.md b/thinkings/README.md index 0f93f8ff1..9678e997b 100644 --- a/thinkings/README.md +++ b/thinkings/README.md @@ -1,7 +1,16 @@ - # 算法专题 - +# 算法专题 + +以下是一些我总结的类型题目,提前搞懂这些东西对之后的做题很有帮助,强烈建议先掌握。另外我的 91 天学算法也对专题进行了更细粒度的整理,具体参见[91 天学算法](../91/README.md) + +首先基础的数据结构大家是必须掌握的,其次就是暴力法。暴力法也是算法,只不过我们追求的肯定是性能更好的算法。因此了解暴力法的算法瓶颈以及各种数据结构的特点就很重要, 这样你可以可以根据这些知识去一步步逼近最优解。 + +再之后就是必须掌握的算法。比如搜索算法是必须掌握的,搜索算法的范围很广,但是核心就是搜索的,不同的算法在于搜索的方式不同,典型的就是 BFS 和 DFS,当然二分法本质上也是一种搜索算法。 + +还有就是暴力优化法也是必须掌握的,和搜索一样,范围很广。 有剪枝, 空间换时间等。 其中空间换时间又有很多,比如哈希表, 前缀树等等。 + +围绕这个思想去学习, 就不会差太多,其他我就不多说,大家慢慢体会。 + - [数据结构](basic-data-structure.md) -- [基础算法](basic-algorithm.md) - [二叉树的遍历](binary-tree-traversal.md) - [动态规划](dynamic-programming.md) - [哈夫曼编码和游程编码](run-length-encode-and-huffman-encode.md) @@ -17,4 +26,4 @@ - [最大公约数](GCD.md) - [并查集](union-find.md) - [前缀和](prefix.md) -- [平衡二叉树专题](balanced-tree.md) \ No newline at end of file +- [平衡二叉树专题](balanced-tree.md) diff --git a/thinkings/balanced-tree.md b/thinkings/balanced-tree.md index f5ad38545..8445d736c 100644 --- a/thinkings/balanced-tree.md +++ b/thinkings/balanced-tree.md @@ -305,6 +305,6 @@ class Solution: > 小提示 3: 链表的操作需要特别注意环的存在。 -更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 30K star 啦。 +更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 diff --git a/thinkings/greedy.md b/thinkings/greedy.md index 3576b6292..79ab1f2b6 100644 --- a/thinkings/greedy.md +++ b/thinkings/greedy.md @@ -264,6 +264,6 @@ class Solution: **复杂度分析** -- 时间复杂度:时间复杂度取决 l 和 r,也就是说取决于 ranges 数组的值,假设 ranges 的平均大小为 Size 的话,那么时间复杂度为 $O(N \* Size)$。 +- 时间复杂度:时间复杂度取决 l 和 r,也就是说取决于 ranges 数组的值,假设 ranges 的平均大小为 Size 的话,那么时间复杂度为 $O(N * Size)$。 - 空间复杂度:我们使用了 furthest 数组, 因此空间复杂度为 $O(N)$。