提交 bb2a96fa 编写于 作者: L lucifer

feat: 使用新浪图床

上级 e97d8ef0
......@@ -13,7 +13,7 @@
---
![leetcode.jpeg](./assets/leetcode.jpeg)
![leetcode.jpeg](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwf4xivj30dw0780sm.jpg)
This essay records the course of and my emotion to this project from initialization to 10,000 stars.
[Milestone for 10,000+ stars](./thanksGiving.md)
......@@ -22,7 +22,7 @@ If you are interested in this project, **do not mean your star**. This project w
## Introduction
![leetcode.jpeg](./assets/leetcode.jpeg)
![leetcode.jpeg](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwf4xivj30dw0780sm.jpg)
LeetCode Solutions: A Journey of Problem Solving.
......@@ -55,7 +55,7 @@ If you want to do some contributions or collaborations, just feel free to contac
- Here will be the place to update Anki Flashcards in the future as well.
- Here is a mind mapping graph showing the summary of categorizations of problems that are questioned frequently in interviews. We could analyze according to the information in the graph.
![leetcode-zhihu](./assets//leetcode-zhihu.jpg)
![leetcode-zhihu](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwgi53bj30k00jx0te.jpg)
(Picture credited by [LeetCode-cn](https://www.zhihu.com/question/24964987/answer/586425979).)
......@@ -80,15 +80,15 @@ The data structures mainly include:
[0547.friend-circles](./problems/547.friend-circles-en.md) :
![friend circle BFS](./assets/problems/547.friend-circle-bfs.png)
![friend circle BFS](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwh1getj30u0140tdc.jpg)
[backtrack problems](./problems/90.subsets-ii-en.md):
![backtrack](./assets/problems/backtrack.png)
![backtrack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwhwowgj30n20nptas.jpg)
[0454.4-sum-ii](./problems/454.4-sum-ii.en.md) :
![454.4-sum-ii](./assets/problems/454.4-sum-ii.png)
![454.4-sum-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwivf65j30le0deab3.jpg)
## Portals
......@@ -173,11 +173,11 @@ We're still on the early stage, so feedback from community is very welcome. For
### QQ (For China Region)
![qq-group-chat](./assets/qq-group-chat.png)
![qq-group-chat](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwje9plj3060060wel.jpg)
### WeChat (For China Region)
![wechat-group-chat](./assets/wechat-group-chat.jpeg)
![wechat-group-chat](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwjrk6ij30e80e875j.jpg)
(Add this bot and reply "leetcode" to join the group.)
......
......@@ -20,7 +20,7 @@
---
![leetcode.jpeg](./assets/leetcode.jpeg)
![leetcode.jpeg](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluelm27rj30dw0780sm.jpg)
- 2019-07-10 :[纪念项目 Star 突破 1W 的一个短文](./thanksGiving.md), 记录了项目的"兴起"之路,大家有兴趣可以看一下,如果对这个项目感兴趣,请**点击一下 Star**, 项目会**持续更新**,感谢大家的支持。
......@@ -29,7 +29,7 @@
- 2020-04-12: [项目突破三万 Star](./thanksGiving3.md)
- 2020-04-14: 官网`力扣加加`上线啦 💐💐💐💐💐,有专题讲解,每日一题,下载区和视频题解,后续会增加更多内容,还不赶紧收藏起来?地址:http://leetcode-solution.cn/
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gdvenxvjlsj30z90dtdhw.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluemaoj3j30z90dtmy5.jpg)
## 非科学人士看过来
......@@ -85,7 +85,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- 这里有一张互联网公司面试中经常考察的问题类型总结的思维导图,我们可以结合图片中的信息分析一下。
![leetcode-zhihu](./assets//leetcode-zhihu.jpg)
![leetcode-zhihu](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluennxvrj30k00jx0te.jpg)
(图片来自 leetcode)
......@@ -110,7 +110,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
[0042.trapping-rain-water](./problems/42.trapping-rain-water.md):
![0042.trapping-rain-water](./assets/problems/42.trapping-rain-water-1.png)
![0042.trapping-rain-water](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlueqdwy3j30bg04hmx3.jpg)
[0547.friend-circles](./problems/547.friend-circles-en.md):
......@@ -375,11 +375,11 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后
- [每日一题汇总](./daily/)
![每日一题汇总](./assets/daily-summary.png)
![每日一题汇总](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluequftmj30p80nrmzc.jpg)
- [每日一题认领区](https://github.com/azl397985856/leetcode/projects/1)
![每日一题认领区](./assets/daily-board.png)
![每日一题认领区](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlues83n0j315f0i8gug.jpg)
### 计划
......
......@@ -52,7 +52,7 @@ function sqrt(num) {
也就是说,函数上任一点(x,f(x))处的切线斜率是2x。
那么,x-f(x)/(2x)就是一个比x更接近的近似值。代入 f(x)=x^2-a得到x-(x^2-a)/(2x),也就是(x+a/x)/2。
![2019-06-27](../assets/daily/2019-06-27.gif)
![2019-06-27](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludzm5xsg30ip0dct9s.gif)
(图片来自Wikipedia)
......
......@@ -28,7 +28,7 @@
这个题目解释起来比较费劲,我在网上找了一个现成的图来解释一下:
![weight-ball](../assets/daily/weight-ball.jpg)
![weight-ball](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue317j6j30d80dcta4.jpg)
图中“1+”是指“1号小球为重”这一可能性。“1-”是指“1号小球为轻”这一可能性。
一开始一共有24种可能性。
......
......@@ -14,7 +14,7 @@
```
![2019-07-23](../assets/daily/2019-07-23.jpeg)
![2019-07-23](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludxwb1aj30py1hc0tr.jpg)
## 参考答案
......
......@@ -8,7 +8,7 @@
## 题目描述
![2019-07-26](../assets/daily/2019-07-26.jpeg)
![2019-07-26](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludytrtlj30py1hcas1.jpg)
## 参考答案
......
......@@ -37,7 +37,7 @@ Example 2:
2. row->col、col->row 的切换都伴随读取的初始位置的变化;
3. 结束条件是row头>row尾或者col顶>col底
![剥洋葱](../assets/problems/54.spiral-matrix.jpg)
![剥洋葱](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue0ni96j30b00bdq35.jpg)
时间复杂度O(m*n), 空间复杂度O(1)
......
......@@ -22,7 +22,7 @@
那么沿着这条纬线(记为E纬线)上任意一点向东走一英里,始终会回到原点,只是走的圈数不同而已。
根据题目倒推,在这条纬线以北一英里存在一条纬线(记为N纬线),从N纬线的任意一点向南一英里到达E纬线W点,沿着E纬线向东一英里,必会回到W点,再向北走一英里恰好可以回到起点。北极点可能包含在这个集合中,也可能不在。
如下图示供参考:
![earth-problem](../assets/daily/2019-07-30.jpg)
![earth-problem](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue5gt6uj30u01441l0.jpg)
所以答案是无数个点
......
......@@ -61,7 +61,7 @@ m 和 n =150,肯定超时。
最后将探测结果进行合并即可。合并的条件就是当前单元既能流入太平洋又能流入大西洋。
![集合](https://user-images.githubusercontent.com/5937331/63209454-7c921a80-c113-11e9-8d74-82d0476b8828.png)
![集合](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue21s7aj30dw08cglo.jpg)
扩展:
如果题目改为能够流入大西洋或者太平洋,我们只需要最后合并的时候,条件改为求或即可
......
......@@ -42,7 +42,7 @@ https://leetcode-cn.com/problems/symmetric-tree/
看到这题的时候,我的第一直觉是 DFS。然后我就想:`如果左子树是镜像,并且右子树也是镜像,是不是就说明整体是镜像?`。经过几秒的思考, 这显然是不对的,不符合题意。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfbmeoo45bj31200iuabh.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu96e83wj31200iugme.jpg)
很明显其中左子树中的节点会和右子树中的节点进行比较,我把比较的元素进行了颜色区分,方便大家看。
......@@ -52,7 +52,7 @@ https://leetcode-cn.com/problems/symmetric-tree/
这种方法可行,但是需要 N 的空间(N 为节点总数)。我想到如果两者可以同时进行遍历,是不是就省去了哈希表的开销。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfbso8u6myj31a30u0tf5.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9a7sy7j31a30u0408.jpg)
如果不明白的话,我举个简单例子:
......@@ -112,4 +112,4 @@ class Solution:
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfaq00p8stj30x20iw7b0.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9b4p9ej30x20iwjtf.jpg)
......@@ -45,7 +45,7 @@ L + M <= A.length <= 1000
题目中要求在前N(数组长度)个数中找出长度分别为L和M的非重叠子数组之和的最大值, 因此, 我们可以定义数组A中前i个数可构成的非重叠子数组L和M的最大值为SUMM[i], 并找到SUMM[i]和SUMM[i-1]的关系, 那么最终解就是SUMM[N]. 以下为图解:
![1031.Maximum Sum of Two Non-Overlapping Subarrays](../assets/problems/1031.maximum-sum-of-two-non-overlapping-subarrays.png)
![1031.Maximum Sum of Two Non-Overlapping Subarrays](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu02o63mj30iz0m9420.jpg)
## 关键点解析
......
......@@ -72,7 +72,7 @@ If you can't figure out how to get the index of the left and right child, please
- according to the properties of preoder traversal, all right sub-tree nodes are behine all left sub-tree nodes. The length of left sub-tree can help us to divide left and right sub-trees.
- the length of left sub-tree can be find in the inorder traversal. The location of current node is `inRoot`(or whatever your call it). The start index of current inorder array is `inStart`(or whatever your call it). So, the lenght of the left sub-tree is `leftChldLen = inRoot - inStart`.
![explain](../assets/problems/105.index_explain.jpg)
![explain](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyu9s05j30o00hnq4y.jpg)
## 代码/Code
......
......@@ -37,7 +37,7 @@ https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/
形象一点来看就像你提起一根绳子,从中点提的话才能使得两边绳子长度相差最小。
![image.png](https://pic.leetcode-cn.com/c24c2280aff65bfa06f94dc000bd7475a100903e5c889ee6ebafa014fd63fe3e-image.png)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltysdgtvj30nj0hv3z2.jpg)
## 关键点
......
......@@ -9,7 +9,7 @@ https://leetcode-cn.com/problems/container-with-most-water/description/
说明:你不能倾斜容器,且 n 的值至少为 2。
 
![11.container-with-most-water-question](../assets/problems/11.container-with-most-water-question.jpg)
![11.container-with-most-water-question](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4wyztmj30m90anwep.jpg)
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
......@@ -62,7 +62,7 @@ return max;
如图:
![11.container-with-most-water](../assets/problems/11.container-with-most-water.png)
![11.container-with-most-water](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4xr7ovj30bm0gct9b.jpg)
比如我们计算 n 面积的时候,假如左侧的线段高度比右侧的高度低,那么我们通过左移右指针来将长度缩短为 n-1 的做法是没有意义的,
因为`新的形成的面积变成了(n-1) * heightOfLeft 这个面积一定比刚才的长度为 n 的面积 (n * heightOfLeft) 小`
......@@ -154,4 +154,4 @@ class Solution:
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4yqnsgj30p00dwt9t.jpg)
......@@ -10,7 +10,7 @@ https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/description
而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gaihhb1ysbj310p0gu3zx.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu59hpv3j310p0gumxz.jpg)
给你树上某一个节点的标号 label,请你返回从根节点到该标号为 label 节点的路径,该路径是由途经的节点标号所组成的。
......@@ -37,17 +37,17 @@ https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/description
如果是这样的话,这道题应该是 easy 难度,代码也不难写出。我们继续考虑之字形。我们不妨先观察一下,找下规律。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gaihn0ktanj30lu093gme.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5au9j8j30lu093gmm.jpg)
以上图最后一行为例,对于 15 节点,之字变换之前对应的应该是 8 节点。14 节点对应的是 9 节点。。。
全部列举出来是这样的:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gaihota82cj30mk0b6wfp.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5d6os7j30mk0b6wfw.jpg)
我们发现之字变换前后的 label 相加是一个定值。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gaihpnlpicj309b08dmxl.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5f240wj309b08dmxj.jpg)
因此我们只需要求解出每一层的这个定值,然后减去当前值就好了。(注意我们不需要区分偶数行和奇数行)
问题的关键转化为求解这个定值,这个定值其实很好求,因为每一层的最大值和最小值我们很容易求,而最大值和最小值的和正是我们要求的这个数字。
......
......@@ -41,7 +41,7 @@ Return:
我们先来看下通用解法的解题思路,我画了一张图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyrnqi82ej31190u0e81.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwyr0bkj31190u0jw4.jpg)
> 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。
......
......@@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/descriptio
> 红色竖线表示的是绝对值的符号
![](https://tva1.sinaimg.cn/large/006tNbRwly1gamo3dx1bej30q003y74f.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu154cgej30q003y3yv.jpg)
我们对其进行分类讨论,有如下八种情况:
......@@ -45,7 +45,7 @@ https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/descriptio
> |i - j| 两种情况
> 因此一共是 2 \* 2 \* 2 = 8 种
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gamosnsknej30tg0viq6w.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu1c4km1j30tg0viq7v.jpg)
由于 i 和 j 之前没有大小关系,也就说二者可以相互替代。因此:
......@@ -58,11 +58,11 @@ https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/descriptio
为了方便,我们将 i 和 j 都提取到一起:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gamp5sizefj30qs0g6gmx.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu1j6ueoj30qs0g6di2.jpg)
容易看出等式的最大值就是前面的最大值,和后面最小值的差值。如图:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gamp9c3g9lj30r20kcabx.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu1oczs3j30r20kctb5.jpg)
再仔细观察,会发现前面部分和后面部分是一样的,原因还是上面所说的 i 和 j 可以互换。因此我们要做的就是:
......@@ -97,17 +97,17 @@ class Solution:
### 思路
![](https://tva1.sinaimg.cn/large/006tNbRwly1gampnn032tj308l0a8mxl.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu23wcsoj308l0a8aak.jpg)
(图来自: https://zh.wikipedia.org/wiki/%E6%9B%BC%E5%93%88%E9%A0%93%E8%B7%9D%E9%9B%A2)
一维曼哈顿距离可以理解为一条线上两点之间的距离: |x1 - x2|,其值为 max(x1 - x2, x2 - x1)
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gampr362oaj30l004mdfv.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2729n3j30l004mwel.jpg)
在平面上,坐标(x1, y1)的点 P1 与坐标(x2, y2)的点 P2 的曼哈顿距离为:|x1-x2| + |y1 - y2|,其值为 max(x1 - x2 + y1 - y2, x2 - x1 + y1 - y2, x1 - x2 + y2 - y1, x2 -x1 + y2 - y1)
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gampwhua9fj30rq0lmdgl.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu29xa0jj30rq0lmwga.jpg)
然后这道题目是更复杂的三维曼哈顿距离,其中(i, arr[i], arr[j])可以看作三位空间中的一个点,问题转化为曼哈顿距离最远的两个点的距离。
延续上面的思路,|x1-x2| + |y1 - y2| + |z1 - z2|,其值为 :
......@@ -186,6 +186,6 @@ class Solution:
- [1030. 距离顺序排列矩阵单元格](https://leetcode-cn.com/problems/matrix-cells-in-distance-order/)
![](https://tva1.sinaimg.cn/large/006tNbRwly1gamq577lgsj30xd0jzwjb.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2h4bnaj30xd0jzgom.jpg)
- [1162. 地图分析](https://leetcode-cn.com/problems/as-far-from-land-as-possible/)
......@@ -37,7 +37,7 @@ pipes[i][0] != pipes[i][1]
## 思路
![example 1](../assets/problems/1168.optimize-water-distribution-in-a-village-example1.png)
![example 1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0bzlucj30ci0bc3z0.jpg)
题意,在每个城市打井需要一定的花费,也可以用其他城市的井水,城市之间建立连接管道需要一定的花费,怎么样安排可以花费最少的前灌溉所有城市。
......@@ -65,7 +65,7 @@ pipes[i][0] != pipes[i][1]
如图:
![minimum cost](../assets/problems/1168.optimize-water-distribution-in-a-village-1.png)
![minimum cost](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0jq6djj31400u0nfs.jpg)
从图中可以看到,最后所有的节点都是连通的。
......
......@@ -34,7 +34,7 @@ pipes[i][0] != pipes[i][1]
```
example 1 pic:
![example 1](../assets/problems/1168.optimize-water-distribution-in-a-village-example1.png)
![example 1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltymocpgj30ci0bc3z0.jpg)
## Solution
......@@ -61,7 +61,7 @@ For example:`n = 5, wells=[1,2,2,3,2], pipes=[[1,2,1],[2,3,1],[4,5,7]]`
As below pic:
![minimum cost](../assets/problems/1168.optimize-water-distribution-in-a-village-1.png)
![minimum cost](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyopr5zj31400u0nfs.jpg)
From pictures, we can see that all nodes already connected with minimum costs.
......
......@@ -36,7 +36,7 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
用图表示的话就是这样:
![](https://tva1.sinaimg.cn/large/0082zybply1gbx7rzp9e1j30jg0c23zs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6k05dqj30jg0c20tf.jpg)
## 关键点解析
......
......@@ -45,7 +45,7 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
用图表示的话就是这样:
![122.best-time-to-buy-and-sell-stock-ii](../assets/problems/122.best-time-to-buy-and-sell-stock-ii.png)
![122.best-time-to-buy-and-sell-stock-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8sjjprj30ff0bv0te.jpg)
## 关键点解析
......
......@@ -57,12 +57,12 @@ https://leetcode-cn.com/problems/airplane-seat-assignment-probability/descriptio
此时的问题转化关系如图:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gb12n0omuuj31bc0ju405.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxcat1fj31bc0jutc4.jpg)
(红色表示票丢的人)
整个过程分析:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gb12nhestaj318u0bg76f.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxcxtmvj318u0bgtbe.jpg)
### 代码
......
......@@ -47,11 +47,11 @@ Output: 42
首先是官网给的两个例子:
![124.binary-tree-maximum-path-sum](../assets/problems/124.binary-tree-maximum-path-sum.jpg)
![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaht4drj30kh07pq3p.jpg)
接着是我自己画的一个例子:
![124.binary-tree-maximum-path-sum](../assets/problems/124.binary-tree-maximum-path-sum-1.jpg)
![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluai4m6dj30hu0cdq46.jpg)
大家可以结合上面的demo来继续理解一下path, 除非你理解了path,否则不要往下看。
......
......@@ -39,11 +39,11 @@ Output: false
拿“noon”这样一个回文串来说,我们的判断过程是这样的:
![125.valid-palindrome-1](../assets/problems/125.valid-palindrome-1.png)
![125.valid-palindrome-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxv0l6lj30fp0883yo.jpg)
拿“abaa”这样一个不是回文的字符串来说,我们的判断过程是这样的:
![125.valid-palindrome-2](../assets/problems/125.valid-palindrome-2.png)
![125.valid-palindrome-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxzbhiqj30ff07y74k.jpg)
......
......@@ -82,7 +82,7 @@ class Solution:
### 思路
我们仔细观察矩阵会发现,其实这样的矩阵迁移是有规律的。 如图:
![image](https://user-images.githubusercontent.com/12479470/72203575-4f6e4c00-34a8-11ea-8765-03fc856d4ea6.png)
![image](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluajlvo1j30us0u0439.jpg)
因此这个问题就转化为我们一直的一维矩阵转移问题,LeetCode 也有原题[189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/),同时我也写了一篇文章[文科生都能看懂的循环移位算法](https://lucifer.ren/blog/2019/12/11/rotate-list/)专门讨论这个,最终我们使用的是三次旋转法,相关数学证明也有写,很详细,这里不再赘述。
......
......@@ -20,7 +20,7 @@ bool find(int target) 判断目标值 target 是否存在于还原后的二
示例 1:
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gasy4qroxoj308w03b3yi.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua6htirj308w03bdfo.jpg)
输入:
["FindElements","find","find"]
......@@ -33,7 +33,7 @@ findElements.find(1); // return False
findElements.find(2); // return True
示例 2:
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gasy5mlo3mj30b405iwep.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua84ataj30b405idfu.jpg)
输入:
["FindElements","find","find","find"]
......@@ -47,7 +47,7 @@ findElements.find(3); // return True
findElements.find(5); // return False
示例 3:
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gasy5sr25yj308i07maa8.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua8rj84j308i07m3yh.jpg)
输入:
["FindElements","find","find","find","find"]
......@@ -188,12 +188,12 @@ class FindElements:
如果我们把树中的数全部加 1 会怎么样?
![](https://tva1.sinaimg.cn/large/006tNbRwly1gasypfuvuvj30rs0kudjr.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaaphnnj30rs0kuwhb.jpg)
(图参考 https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/discuss/431229/Python-Special-Way-for-find()-without-HashSet-O(1)-Space-O(logn)-Time)
仔细观察发现,每一行的左右子树分别有不同的前缀:
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gasz0x09koj312y0sgnnt.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluackit8j312y0sgtdy.jpg)
Ok,那么算法就来了。为了便于理解,我们来举个具体的例子,比如 target 是 9,我们首先将其加 1,二进制表示就是 1010。不考虑第一位,就是 010,我们只要:
......
......@@ -47,7 +47,7 @@ https://leetcode-cn.com/problems/greatest-sum-divisible-by-three/description/
更多回溯题目,可以访问上方链接查看(可以使用一套模板搞定):
![](https://tva1.sinaimg.cn/large/006tNbRwly1gatuu0yfh8j30f60c4ab3.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu49wysqj30f60c4my0.jpg)
### 代码
......@@ -89,11 +89,11 @@ class Solution:
以题目中的例 1 为例:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gatvdy1zb1j30u00x2wgx.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4dsqzhj30u00x2n0u.jpg)
以题目中的例 2 为例:
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gatvem1xm4j30u00xvq59.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4l71rzj30u00xvwia.jpg)
### 代码
......@@ -175,7 +175,7 @@ class Solution:
我在[数据结构与算法在前端领域的应用 - 第二篇](https://lucifer.ren/blog/2019/09/19/algorthimn-fe-2/) 中讲到了有限状态机。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gatub3vftxj30eq0bfta0.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4nj6u8j30eq0bfdgl.jpg)
状态机表示若干个状态以及在这些状态之间的转移和动作等行为的数学模型。通俗的描述状态机就是定义了一套状态変更的流程:状态机包含一个状态集合,定义当状态机处于某一个状态的时候它所能接收的事件以及可执行的行为,执行完成后,状态机所处的状态。
......
......@@ -55,12 +55,12 @@ Therefore, sum = 495 + 491 + 40 = 1026.
整个过程如图所示:
![129.sum-root-to-leaf-numbers-1](../assets/problems/129.sum-root-to-leaf-numbers-1.jpg)
![129.sum-root-to-leaf-numbers-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu66bb27j30k10a6dgx.jpg)
那么数字具体的计算逻辑,如图所示,相信大家通过这个不难发现规律:
![129.sum-root-to-leaf-numbers-2](../assets/problems/129.sum-root-to-leaf-numbers-2.jpg)
![129.sum-root-to-leaf-numbers-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu67b2mkj30mo0agmys.jpg)
## 关键点解析
......
......@@ -35,7 +35,7 @@ Surrounded regions shouldn’t be on the border, which means that any 'O' on the
我们需要将所有被X包围的O变成X,并且题目明确说了边缘的所有O都是不可以变成X的。
![130.surrounded-regions](../assets/problems/130.surrounded-regions-1.jpg)
![130.surrounded-regions](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7kk7n3j30ee09waap.jpg)
其实我们观察会发现,我们除了边缘的O以及和边缘O连通的O是不需要变成X的,其他都要变成X。
......@@ -46,7 +46,7 @@ Surrounded regions shouldn’t be on the border, which means that any 'O' on the
> 我将`边缘的O以及和边缘O连通的O` 标记为了 "A"
![130.surrounded-regions](../assets/problems/130.surrounded-regions-2.jpg)
![130.surrounded-regions](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7ms9mij30mr0b9q4c.jpg)
......
......@@ -33,7 +33,7 @@ Output:
这里我画了一个图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyrnqi82ej31190u0e81.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty0bvj4j31190u0jw4.jpg)
> 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。
......
......@@ -80,7 +80,7 @@ class Solution:
这道题是前缀对前缀异或,我们利用了异或的性质 `x ^ y ^ x = y`
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gaqll5r048j30fm0bfglz.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxsg8v8j30fm0bf74w.jpg)
### 代码
......@@ -167,6 +167,6 @@ public:
- [303. 区域和检索 - 数组不可变](https://leetcode-cn.com/problems/range-sum-query-immutable/description/)
![](https://tva1.sinaimg.cn/large/006tNbRwly1gaql7eqyg6j30u00ft0vx.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxt83dtj30u00ftac4.jpg)
- [1186.删除一次得到子数组最大和](https://lucifer.ren/blog/2019/12/11/leetcode-1186/)
......@@ -17,7 +17,7 @@ https://leetcode-cn.com/problems/find-the-city-with-the-smallest-number-of-neigh
```
![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbh9v5ygtsj20qo0k0aap.jpg)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubmx9n7j30qo0k03zm.jpg)
```
......@@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/find-the-city-with-the-smallest-number-of-neigh
```
![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbh9vg1w43j20qo0k0js8.jpg)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubnw6l9j30qo0k0q4c.jpg)
```
......
......@@ -48,26 +48,26 @@ Output: false
我们用图来感受一下:
![139.word-break-1](../assets/problems/139.word-break-1.png)
![139.word-break-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu370c2hj30n60cnmy6.jpg)
没有明白也没有关系,我们分步骤解读一下:
(以下的图左边都代表s,右边都是dict,灰色代表没有处理的字符,绿色代表匹配成功,红色代表匹配失败)
![139.word-break-2](../assets/problems/139.word-break-2.png)
![139.word-break-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu37ydiwj30aw0b1mxc.jpg)
![139.word-break-3](../assets/problems/139.word-break-3.png)
![139.word-break-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3f3l6kj30bt0akdg0.jpg)
![139.word-break-4](../assets/problems/139.word-break-4.png)
![139.word-break-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3mmjmtj30mw09ymxp.jpg)
![139.word-break-5](../assets/problems/139.word-break-5.png)
![139.word-break-5](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3qr7ppj30n90kqabg.jpg)
上面分步解释了算法的基本过程,下面我们感性认识下这道题,我把它比喻为
你正在`往一个老式手电筒🔦中装电池`
![139.word-break-6](../assets/problems/139.word-break-6.png)
![139.word-break-6](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3rqvffj30mz0frwg3.jpg)
## 代码
......
......@@ -35,7 +35,7 @@ Follow up: Recursive solution is trivial, could you do it iteratively?
> 其他树的非递归遍历可没这么简单
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg7v5hztd2j30zu0ntwiu.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxumvwfj30zu0nttak.jpg)
(迭代 VS 递归)
......
......@@ -118,13 +118,13 @@ for i in 1 to N + 1:
那么如果我们不降序遍历会怎么样呢?
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfaqt9hh8hj30uy0giabo.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubxof5ej30uy0gi758.jpg)
如图橙色部分表示已经遍历的部分,而让我们去用[j - cost[i - 1]] 往前面回溯的时候,实际上回溯的是 dp[i]j - cost[i - 1]],而不是 dp[i - 1]j - cost[i - 1]]。
如果是降序就可以了,如图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfaqtkc1cmj30u80fcmyo.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubynqxqj30u80fcgmi.jpg)
这个明白的话,我们继续思考为什么完全背包就要不降序了呢?
......@@ -177,12 +177,12 @@ class Solution:
最后贴几个我写过的背包问题,让大家看看历史是多么的相似。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfaqty4tg3j31iq0sotdg.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubzm45mj31iq0sotbx.jpg)
[322. 硬币找零(完全背包问题)](https://github.com/azl397985856/leetcode/blob/master/problems/322.coin-change.md)
> 这里内外循环和本题正好是反的,我只是为了"秀技"(好玩),实际上在这里对答案并不影响。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfaqu6xzg5j31go0gwq4o.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluc31c32j31go0gwq3z.jpg)
[518. 零钱兑换 II](https://github.com/azl397985856/leetcode/blob/master/problems/518.coin-change-2.md)
> 这里内外循环和本题正好是反的,但是这里必须这么做,否则结果是不对的,具体可以点进去链接看我那个题解
......@@ -217,4 +217,4 @@ for j in V to 0:
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfaq00p8stj30x20iw7b0.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluc9tkv6j30x20iwjtf.jpg)
......@@ -44,7 +44,7 @@ A solution set is:
思路如图所示:
![15.3-sum](../assets/problems/15.3-sum.png)
![15.3-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyijyb3j30l00e2q3p.jpg)
> 在这里之所以要排序解决是因为, 我们算法的瓶颈在这里不在于排序,而在于 O(N^2),如果我们瓶颈是排序,就可以考虑别的方式了
......
......@@ -51,7 +51,7 @@ var maxProduct = function(nums) {
前面说了`最小值(比如-20)乘以一个比较小的数(比如-10)可能就会很大` 。因此我们需要同时记录乘积最大值和乘积最小值,然后比较元素和这两个的乘积,去不断更新最大值。当然,我们也可以选择只取当前元素。因此实际上我们的选择有三种,而如何选择就取决于哪个选择带来的价值最大(乘积最大或者最小)。
![](https://pic.leetcode-cn.com/7d39989d10d982d44cbd6b6f693cf5171865c0654f7c3754e27ec1afc2c0de5d.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0be8nej30gr08kjru.jpg)
这种思路的解法由于只需要遍历一次,其时间复杂度是O(n),代码见下方代码区。
......
......@@ -32,7 +32,7 @@ minStack.getMin(); --> Returns -2.
符合直觉的方法是,每次对栈进行修改操作(push和pop)的时候更新最小值。 然后getMin只需要返回我们计算的最小值即可,
top也是直接返回栈顶元素即可。 这种做法每次修改栈都需要更新最小值,因此时间复杂度是O(n).
![](https://pic.leetcode-cn.com/7beed41b8dc0325445721a36b7db34e1af902441b67996d2eeadcb1f5a5e33d9.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucity87j30d609ggls.jpg)
是否有更高效的算法呢?答案是有的。
......@@ -54,8 +54,8 @@ pop或者top的时候:
- 如果栈顶元素大于0,说明它对最小值`没有影响`,上一个最小值就是上上个最小值。
![](https://pic.leetcode-cn.com/7da0473d92d70bb47ce7b62303c062e5f517b09d1bf501c4ad341b65415d5c43.jpg)
![](https://pic.leetcode-cn.com/aefec54238c942c484837ea6c724304fb179d3d64f110481d955d9eea65c4fc5.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucqck9mj30ji0k1gn0.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucsjh58j30ht0b4mxr.jpg)
## 关键点
......
......@@ -102,4 +102,4 @@ class Solution:
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0wr0tsj30p00dwt9t.jpg)
......@@ -32,7 +32,7 @@ Output: 2
投票算法的原理是通过不断消除不同元素直到没有不同元素,剩下的元素就是我们要找的元素。
![](https://tva1.sinaimg.cn/large/0082zybply1gbv38hcpf2j30mz0cjjt4.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7i1c8tj30mz0cjwfk.jpg)
## 关键点解析
......@@ -85,4 +85,4 @@ class Solution:
欢迎关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7jhx8dj31bi0hcq5s.jpg)
......@@ -32,16 +32,16 @@ Note: Your solution should be in logarithmic time complexity.
通过观察,我们发现如果想要结果末尾是0,必须是分解质因数之后,2 和 5 相乘才行,同时因数分解之后发现5的个数远小于2,
因此我们只需要求解这n数字分解质因数之后一共有多少个5即可.
![172.factorial-trailing-zeroes-2](../assets/problems/172.factorial-trailing-zeroes-2.png)
![172.factorial-trailing-zeroes-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubdkzp7j30i10970t2.jpg)
如图如果n为30,那么结果应该是图中红色5的个数,即7。
![172.factorial-trailing-zeroes-1](../assets/problems/172.factorial-trailing-zeroes-1.png)
![172.factorial-trailing-zeroes-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubf2c3fj30hr0b4aar.jpg)
我们的结果并不是直接f(n) = n / 5, 比如n为30, 25中是有两个5的。
类似,n为150,会有7个这样的数字,通过观察我们发现规律`f(n) = n/5 + n/5^2 + n/5^3 + n/5^4 + n/5^5+..`
![172.factorial-trailing-zeroes-3](../assets/problems/172.factorial-trailing-zeroes-3.png)
![172.factorial-trailing-zeroes-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubgxccqj30h3091t9i.jpg)
如果可以发现上面的规律,用递归还是循环实现这个算式就看你的了。
## 关键点解析
......
......@@ -40,7 +40,7 @@ Could you do this in one pass?
将 p 的下一个节点指向下下个节点
![19.removeNthNodeFromEndOfList](../assets/19.removeNthNodeFromEndOfList.gif)
![19.removeNthNodeFromEndOfList](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludrrxbjg30qn0ezajr.gif)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
......
......@@ -116,7 +116,7 @@ class Solution(object):
## 扩展
可以使用位操作来达到目的。例如8位的整数21:
![number-of-1-bits](../assets/problems/191.number-of-1-bits.png)
![number-of-1-bits](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyhhz7mj308007w0sx.jpg)
C++ Code:
```c++
......
......@@ -48,7 +48,7 @@ Since we always want a larger gain, it's easy to obtain the transition formula:
> Note: For the convenience of calculation, we set both dp[0] and dp[1] to be 0. This way, dp[i] is actually for the i-1<sup>th</sup> house.
We can use the following graph to illustrate the above process:
![198.house-robber](../assets/problems/198.house-robber.png)
![198.house-robber](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluas8wykj30k00bjdh6.jpg)
If we optimize it further, we only need dp[i - 1] and dp[i - 2] when determining each dp[i]. For example, to calculate dp[6], we would only need dp[5] and dp[4], and there's no need to keep dp[3], dp[2], and so on, in memory.
......
......@@ -50,7 +50,7 @@ Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (m
上述过程用图来表示的话,是这样的:
![198.house-robber](../assets/problems/198.house-robber.png)
![198.house-robber](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluatdk9oj30k00bjdh6.jpg)
我们仔细观察的话,其实我们只需要保证前一个 dp[i - 1] 和 dp[i - 2] 两个变量就好了,
比如我们计算到 i = 6 的时候,即需要计算 dp[6]的时候, 我们需要 dp[5], dp[4],但是我们
......
......@@ -19,7 +19,7 @@ Explanation: 342 + 465 = 807.
Define a new variable `carried` that represents the carry value during the calculation, and a new linked list
Traverse the two linked lists from the start to the end simultaneously, and calculate the sum of node value from each linked list. The sum of the result and `carried` would be appended as a new node to the end of the new linked list.
![2.addTwoNumbers](../assets/2.addTwoNumbers.gif)
![2.addTwoNumbers](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludjiguqg30qh0eon5c.gif)
(Image Reference: https://github.com/MisterBooo/LeetCodeAnimation)
......
......@@ -30,7 +30,7 @@ Explanation: 342 + 465 = 807.
设立一个表示进位的变量 carried,建立一个新链表,
把输入的两个链表从头往后同时处理,每两个相加,将结果加上 carried 后的值作为一个新节点到新链表后面。
![2.addTwoNumbers](../assets/2.addTwoNumbers.gif)
![2.addTwoNumbers](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6u8jwyg30qh0eon5c.gif)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
......
......@@ -57,7 +57,7 @@ Output: true
3)若不为对应的左半边括号,反之返回 false
![20.validParentheses](../assets/20.validParentheses.gif)
![20.validParentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyb2lpvg30qo0f0n2n.gif)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
......
......@@ -36,7 +36,7 @@ Output: 3
如图,我们其实就是要求红色区域的个数,换句话说就是求连续区域的个数。
![200.number-of-islands](../assets/problems/200.number-of-islands.jpg)
![200.number-of-islands](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludwu4zlj309y0dgjs0.jpg)
符合直觉的做法是用DFS来解:
......@@ -161,7 +161,7 @@ class Solution:
欢迎关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludxd8jhj31bi0hcq5s.jpg)
## 相关题目
......
......@@ -59,7 +59,7 @@ function computeIndex(c) {
其实不管 insert, search 和 startWith 的逻辑都是差不多的,都是从 root 出发,
找到我们需要操作的 child, 然后进行相应操作(添加,修改,返回)。
![208.implement-trie-prefix-tree-1](../assets/problems/208.implement-trie-prefix-tree-1.png)
![208.implement-trie-prefix-tree-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8zkn7rj30mz0gq406.jpg)
## 关键点解析
......
......@@ -25,7 +25,7 @@ If you have figured out the O(n) solution, try coding another solution of which
用滑动窗口来记录序列, 每当滑动窗口中的 sum 超过 s, 就去更新最小值,并根据先进先出的原则更新滑动窗口,直至 sum 刚好小于 s
![209.minimum-size-subarray-sum](../assets/problems/209.minimum-size-subarray-sum.png)
![209.minimum-size-subarray-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4211x3j30my0kxdh3.jpg)
> 这道题目和 leetcode 3 号题目有点像,都可以用滑动窗口的思路来解决
......@@ -121,7 +121,7 @@ public:
欢迎关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu43kcxpj31bi0hcq5s.jpg)
## 扩展
......
......@@ -40,7 +40,7 @@ search("b..") -> true
关于前缀树,LeetCode 有很多题目。有的是直接考察,让你实现一个前缀树,有的是间接考察,比如本题。前缀树代码见下方,大家之后可以直接当成前缀树的解题模板使用。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gb5dmstsxxj30mz0gqmzh.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwng8wvj30mz0gqdhc.jpg)
由于我们这道题需要考虑特殊字符".",因此我们需要对标准前缀树做一点改造,insert 不做改变,我们只需要改变 search 即可,代码(Python 3):
......
......@@ -49,7 +49,7 @@ words = ["oath","pea","eat","rain"] and board =
刚才我提到了一个关键词“前缀”,我们考虑使用前缀树来优化。使得复杂度降低为$O(h)$, 其中 h 是前缀树深度,也就是最长的字符串长度。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gb5dmstsxxj30mz0gqmzh.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua4m3ofj30mz0gqdhc.jpg)
## 关键点
......
......@@ -45,7 +45,7 @@ You may assume k is always valid, 1 ≤ k ≤ array's length.
扫描一遍数组,最后堆顶就是第`K`大的元素。 直接返回。
例如:
![heap](../assets/problems/215.kth-largest-element-in-an-array-heap.jpg)
![heap](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwuls8wj312q0u0q7x.jpg)
*时间复杂度*`O(n * logk) , n is array length`
*空间复杂度*`O(k)`
......@@ -70,7 +70,7 @@ Quick Select 类似快排,选取pivot,把小于pivot的元素都移到pivot
如下图:
```
![quick select](../assets/problems/215.kth-largest-element-in-an-array-quick-select.jpg)
![quick select](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwvfdvvj30yl0nxwj0.jpg)
*时间复杂度*
- 平均是:`O(n)`
......
......@@ -28,7 +28,7 @@ Output: 4
## 思路
![221.maximal-square](../assets/problems/221.maximal-square-1.jpg)
![221.maximal-square](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludl52xfj30bo09vmxo.jpg)
符合直觉的做法是暴力求解处所有的正方形,逐一计算面积,然后记录最大的。这种时间复杂度很高。
......@@ -42,13 +42,13 @@ Output: 4
dp[2][2]等于1(之前已经计算好了),那么其实这里的瓶颈在于三者的最小值, 即`Min(1, 1, 3)`, 也就是`1`。 那么dp[3][3] 就等于
`Min(1, 1, 3) + 1`
![221.maximal-square](../assets/problems/221.maximal-square-2.jpg)
![221.maximal-square](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludlnra9j30an08xt96.jpg)
dp[i - 1][j - 1]我们直接拿到,关键是`往上和往左进行延伸`, 最直观的做法是我们内层加一个循环去做就好了。
但是我们仔细观察一下,其实我们根本不需要这样算。 我们可以直接用dp[i - 1][j]和dp[i][j -1]
具体就是`Min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1`
![221.maximal-square](../assets/problems/221.maximal-square-3.jpg)
![221.maximal-square](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludm7ilmj30a507sglz.jpg)
事实上,这道题还有空间复杂度O(N)的解法,其中N指的是列数。
大家可以去这个[leetcode讨论](https://leetcode.com/problems/maximal-square/discuss/61803/C%2B%2B-space-optimized-DP)看一下。
......
......@@ -39,9 +39,9 @@ Output: [1,2]
这里画了一个图,大家可以感受一下:
![229.majority-element-ii-1](../assets/problems/229.majority-element-ii-1.jpeg)
![229.majority-element-ii-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltygnjljj31400u0ad9.jpg)
![229.majority-element-ii-1](../assets/problems/229.majority-element-ii-2.jpeg)
![229.majority-element-ii-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyh2s8jj31400u075n.jpg)
......
......@@ -35,7 +35,7 @@ https://leetcode-cn.com/problems/merge-k-sorted-lists/description/
具体我们可以来看一个动画
![23.merge-k-sorted-lists](../assets/problems/23.merge-k-sorted-lists.gif)
![23.merge-k-sorted-lists](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluds9tu0g30go09ajto.gif)
(动画来自 https://zhuanlan.zhihu.com/p/61796021 )
......
......@@ -48,20 +48,20 @@ You may assume that all operations are valid (for example, no pop or peek operat
push之前是这样的:
![232.implement-queue-using-stacks.drawio](../assets/problems/232.implement-queue-using-stacks-1.jpg)
![232.implement-queue-using-stacks.drawio](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu87p5vuj30c2067dg5.jpg)
然后我们将栈中的元素转移到辅助栈:
![232.implement-queue-using-stacks.drawio](../assets/problems/232.implement-queue-using-stacks-2.jpg)
![232.implement-queue-using-stacks.drawio](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu88r2h6j308p07c74k.jpg)
最后将新的元素添加到栈顶。
![232.implement-queue-using-stacks.drawio](../assets/problems/232.implement-queue-using-stacks-3.jpg)
![232.implement-queue-using-stacks.drawio](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu898h4rj309t07074l.jpg)
整个过程是这样的:
![232.implement-queue-using-stacks.drawio](../assets/problems/232.implement-queue-using-stacks-4.jpg)
![232.implement-queue-using-stacks.drawio](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu89mjsjj30jg0czaba.jpg)
## 关键点解析
- 在push的时候利用辅助栈(双栈)
......
......@@ -12,7 +12,7 @@ According to the definition of LCA on Wikipedia: “The lowest common ancestor i
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
```
![236.lowest-common-ancestor-of-a-binary-tree](../assets/problems/236.lowest-common-ancestor-of-a-binary-tree-1.png)
![236.lowest-common-ancestor-of-a-binary-tree](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4oh2jqj305k05aa9z.jpg)
```
Example 1:
......@@ -51,7 +51,7 @@ p and q are different and both values will exist in the binary tree.
对于具体的代码而言就是,我们假设这个树就一个结构,然后尝试去解决,然后在适当地方去递归自身即可。 如下图所示:
![236.lowest-common-ancestor-of-a-binary-tree-2](../assets/problems/236.lowest-common-ancestor-of-a-binary-tree-2.png)
![236.lowest-common-ancestor-of-a-binary-tree-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4pf06vj30n00aiq3o.jpg)
我们来看下核心代码:
......
......@@ -38,7 +38,7 @@ https://leetcode.com/problems/product-of-array-except-self/description/
考虑我们先进行一次遍历, 然后维护一个数组,第i项代表前i个元素(不包括i)的乘积。
然后我们反向遍历一次,然后维护另一个数组,同样是第i项代表前i个元素(不包括i)的乘积。
![238.product-of-array-except-self](../assets/problems/238.product-of-array-except-self.png)
![238.product-of-array-except-self](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7zbobsj30n10c9gma.jpg)
有意思的是第一个数组和第二个数组的反转(reverse)做乘法(有点像向量运算)就是我们想要的运算。
......
......@@ -84,7 +84,7 @@ class Solution:
如果你仔细观察的话,发现双端队列其实是一个递减的一个队列。因此队首的元素一定是最大的。用图来表示就是:
![](https://tva1.sinaimg.cn/large/0082zybply1gbvyn8ufbvj30hb0di75s.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxg29buj30hb0di757.jpg)
## 关键点解析
......
......@@ -29,7 +29,7 @@ Given 1->2->3->4, you should return the list as 2->1->4->3.
7. current 移动两格
8. 重复
![24.swap-nodes-in-pairs](../assets/24.swap-nodes-in-pairs.gif)
![24.swap-nodes-in-pairs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6v237kg30qk0evqbw.gif)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
......
......@@ -36,7 +36,7 @@ Given target = 20, return false.
有没有时间复杂度更好的做法呢? 答案是有,那就是充分运用矩阵的特性(横向纵向都递增),
我们可以从角落(左下或者右上)开始遍历,这样时间复杂度是O(m + n).
![](https://tva1.sinaimg.cn/large/0082zybply1gbrcf58gsqj30ft0b4wfv.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlub9dbyij30ft0b43zd.jpg)
其中蓝色代表我们选择的起点元素, 红色代表目标元素。
......
......@@ -46,7 +46,7 @@ curr = temp;
举例如图:翻转整个链表 `1->2->3->4->null` -> `4->3->2->1->null`
![reverse linked list](../assets/problems/25.reverse-nodes-in-k-groups-1.PNG)
![reverse linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwz9weoj31400u0ahc.jpg)
这里是对每一组(`k个nodes`)进行翻转,
......@@ -64,11 +64,11 @@ curr = temp;
如图所示 步骤 4 和 5: 翻转区间链表区间`(start, end)`
![reverse linked list range in (start, end)](../assets/problems/25.reverse-nodes-in-k-groups-3.png)
![reverse linked list range in (start, end)](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx146hoj30zd0qxjtg.jpg)
举例如图,`head=[1,2,3,4,5,6,7,8], k = 3`
![reverse k nodes in linked list](../assets/problems/25.reverse-nodes-in-k-groups-2.PNG)
![reverse k nodes in linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx3k8x2j312u0u0nnt.jpg)
> **NOTE**: 一般情况下对链表的操作,都有可能会引入一个新的`dummy node`,因为`head`有可能会改变。这里`head 从1->3`,
> `dummy (List(0))`保持不变。
......
......@@ -45,7 +45,7 @@ curr = temp;
For example(as below pic): reverse the whole linked list `1->2->3->4->null` -> `4->3->2->1->null`
![reverse linked list](../assets/problems/25.reverse-nodes-in-k-groups-1.PNG)
![reverse linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty7t8i8j31400u0ahc.jpg)
Here Reverse each group(`k nodes`):
......@@ -63,13 +63,13 @@ Here Reverse each group(`k nodes`):
As below pic show steps 4 and 5, reverse linked list in range `(start, end)`:
![reverse linked list range in (start, end)](../assets/problems/25.reverse-nodes-in-k-groups-3.png)
![reverse linked list range in (start, end)](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty8tkv0j30zd0qxjtg.jpg)
For example(as below pic),`head=[1,2,3,4,5,6,7,8], k = 3`
![reverse k nodes in linked list](../assets/problems/25.reverse-nodes-in-k-groups-2.PNG)
![reverse k nodes in linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty9776uj312u0u0nnt.jpg)
>**NOTE**: Usually we create a `dummy node` to solve linked list problem, because head node may be changed during operation.
......
......@@ -56,7 +56,7 @@ for (int i = 0; i < len; i++) {
- 当快指针走完整个数组后,慢指针当前的坐标加 1 就是数组中不同数字的个数
![26.remove-duplicates-from-sorted-array](../assets/26.remove-duplicates-from-sorted-array.gif)
![26.remove-duplicates-from-sorted-array](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucxqaoyg30qg0esju1.gif)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
......@@ -142,4 +142,4 @@ public:
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucyn5dcj30p00dwt9t.jpg)
......@@ -39,7 +39,7 @@ Input is within the 32-bit signed integer range: [−231, 231 − 1].
题目要求给定一个数字,判断是否为“丑陋数”(ugly number), 丑陋数是指只包含质因子2, 3, 5的正整数。
![263.ugly-number](../assets/problems/263.ugly-number.png)
![263.ugly-number](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxf68kej30hh09fdgd.jpg)
根据定义,我们将给定数字除以2、3、5(顺序无所谓),直到无法整除。
如果得到1,说明是所有因子都是2或3或5,如果不是1,则不是丑陋数。
......
......@@ -50,7 +50,7 @@ Assume we are dealing with an environment which could only store integers within
这种做法简单直观,但是性能却比较差. 下面来介绍一种性能更好的方法。
![29.divide-two-integers](../assets/problems/29.divide-two-integers.png)
![29.divide-two-integers](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluakjnbkj30n20lbjss.jpg)
通过上面这样的分析,我们直到可以使用二分法来解决,性能有很大的提升。
......
......@@ -80,11 +80,11 @@ function findMedian(a) {
比如对于[1,2,3] 求中位数:
![295.find-median-from-data-stream-1](../assets/problems/295.find-median-from-data-stream-1.png)
![295.find-median-from-data-stream-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty0myeij30n0064t8x.jpg)
再比如对于[1,2,3, 4] 求中位数:
![295.find-median-from-data-stream-2](../assets/problems/295.find-median-from-data-stream-2.png)
![295.find-median-from-data-stream-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty1ld04j30mx06ljrm.jpg)
## 关键点解析
- 用两个堆(一个大顶堆,一个小顶堆)来简化时间复杂度
......@@ -210,7 +210,7 @@ this.heap.shift(null);
其实就是为了存储的数据从1开始,这样方便计算。 即对于下标为i的元素, `i >> 1` 一定是父节点的下标。
![295.find-median-from-data-stream-3](../assets/problems/295.find-median-from-data-stream-3.png)
![295.find-median-from-data-stream-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty4xqrrj30n706z3yu.jpg)
> 这是因为我用满二叉树来存储的堆
......
......@@ -37,7 +37,7 @@ Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer
(4)维护一个结果 res,每次用出现过的窗口大小来更新结果 res,最后返回 res 获取结果。
![3.longestSubstringWithoutRepeatingCharacters](../assets/3.longestSubstringWithoutRepeatingCharacters.gif)
![3.longestSubstringWithoutRepeatingCharacters](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubou8hhg30no0dbjvw.gif)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
......@@ -82,4 +82,4 @@ class Solution:
大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubtysc0j30p00dwt9t.jpg)
......@@ -37,7 +37,7 @@ Output: [""]
而且由于题目要求是要删除最少的小括号,因此我们的思路是使用广度优先遍历,而不是深度有限的遍历。
![301.remove-invalid-parentheses](../assets/problems/301.remove-invalid-parentheses.png)
![301.remove-invalid-parentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlude6ok2j30s90fctaa.jpg)
> 没有动图,请脑补
......
......@@ -30,7 +30,7 @@ Here are some examples. Inputs are in the left-hand column and its corresponding
这种题目比较抽象,写几个例子通常会帮助理解问题的规律。我找了几个例子,其中蓝色背景表示的是当前数字找下一个更大排列的时候`需要改变的元素`.
![31.next-permutation](../assets/problems/31.next-permutation-1.jpg)
![31.next-permutation](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4t2qbfj30cx0703yw.jpg)
我们不难发现,蓝色的数字都是从后往前第一个不递增的元素,并且我们的下一个更大的排列
只需要改变蓝色的以及之后部分即可,前面的不需要变。
......@@ -41,18 +41,18 @@ Here are some examples. Inputs are in the left-hand column and its corresponding
另外我们也可以以回溯的角度来思考这个问题,让我们先回溯一次:
![31.next-permutation-2](../assets/problems/31.next-permutation-2.jpg)
![31.next-permutation-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4tmf9vj30d204r74f.jpg)
这个时候可以选择的元素只有2,我们无法组成更大的排列,我们继续回溯,直到如图:
![31.next-permutation-3](../assets/problems/31.next-permutation-3.jpg)
![31.next-permutation-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4ukjgej30go07imxq.jpg)
我们发现我们可以交换4或者2实现变大的效果,但是要保证变大的幅度最小(下一个更大),
我们需要选择最小的,由于之前我们发现后面是从左到右递减的,显然就是交换最右面大于1的。
之后就是不断交换使之幅度最小:
![31.next-permutation-4](../assets/problems/31.next-permutation-4.jpg)
![31.next-permutation-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4vhrisj30h00cmwfn.jpg)
## 关键点解析
- 写几个例子通常会帮助理解问题的规律
......
......@@ -106,7 +106,7 @@ var maxCoins = function (nums) {
- 因为我们最终要求的答案是 dp[0][n + 1],就是戳破虚拟气球之间的所有气球获得的最大值;
- 当 i == j 时,i 和 j 之间是没有气球的,所以枚举的状态很明显是 dp table 的左上部分,也就是 j 大于 i,如下图所示,只给出一部分方便思考。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfckn1vngxj30lk0aodgf.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwkpbhyj30lk0aoaa9.jpg)
(图有错误。图中 dp[k][i] 应该是 dp[i][k],dp[j][k] 应该是 dp[k][j]
> 从上图可以看出,我们需要从下到上,从左到右进行遍历。
......
......@@ -233,7 +233,7 @@ s = '(())())'
4. 根据第 3 条规则来计算的话, 我们发现 dp[5]=0, dp[6]=2, 但是显然, dp[6]应该为 6 才对, 但是我们发现可以将"(())"和"()"进行拼接, 即: dp[i] += dp[i-dp[i]], 即: dp[6] = 2 + dp[6-2] = 2 + dp[4] = 6
根据以上规则, 我们求解 dp 数组的结果为: [0, 0, 0, 2, 4, 0, 6, 0], 其中最长有效括号对的长度为 6. 以下为图解:
![32.longest-valid-parentheses](../assets/problems/32.longest-valid-parentheses.png)
![32.longest-valid-parentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8oq5vxj30pn0cb0vo.jpg)
### 代码
......
......@@ -55,10 +55,10 @@ Output: -1
我们以([6,7,8,1,2,3,4,5], 4)为例讲解一下:
![search-in-rotated-sorted-array-1](../assets/problems/search-in-rotated-sorted-array-1.jpg)
![search-in-rotated-sorted-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucavmv8j30if0b03zp.jpg)
![search-in-rotated-sorted-array-1](../assets/problems/search-in-rotated-sorted-array-2.jpg)
![search-in-rotated-sorted-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucdam5mj30gx0i2jt8.jpg)
## 关键点解析
......
......@@ -34,7 +34,7 @@ Output: false
我们的目标就是`依次`找到三个数字,其顺序是递增的。因此我们的做法可以是依次遍历,
然后维护三个变量,分别记录最小值,第二小值,第三小值。只要我们能够填满这三个变量就返回true,否则返回false。
![334.increasing-triplet-subsequence](../assets/problems/334.increasing-triplet-subsequence.png)
![334.increasing-triplet-subsequence](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu86293pj30n30jdabm.jpg)
## 关键点解析
- 维护三个变量,分别记录最小值,第二小值,第三小值。只要我们能够填满这三个变量就返回true,否则返回false
......
......@@ -56,24 +56,24 @@ https://leetcode-cn.com/problems/self-crossing/
1. 我们画的圈不断增大。
2. 我们画的圈不断减少。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gbepb3y3uwj30te1dagn5.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxh0sygj30te1dajvv.jpg)
(有没有感觉像迷宫?)
这样我们会发现,其实我们画最新一笔的时候,并不是之前画的所有的都需要考虑,我们只需要最近的几个就可以了,实际上是最近的五个,不过不知道也没关系,我们稍后会讲解。
![](https://tva1.sinaimg.cn/large/006tNbRwly1gbepcb2ojwj30to0lamxm.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxhyhumj30to0lamyt.jpg)
红色部分指的是我们需要考虑的,而剩余没有被红色标注的部分则无需考虑。不是因为我们无法与之相交,而是我们`一旦与之相交,则必然我们也一定会与红色标记部分相交`
然而我们画的方向也是不用考虑的。比如我当前画的方向是从左到右,那和我画的方向是从上到下有区别么?在这里是没区别的,不信我帮你将上图顺时针旋转 90 度看一下:
![](https://tva1.sinaimg.cn/large/006tNbRwgy1gbepgmzlopj30mk1cwwfn.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxjatzhj30mk1cwdk7.jpg)
方向对于我们考虑是否相交没有差别。
当我们仔细思考的时候,会发现其实相交的情况只有以下几种:
![](https://tva1.sinaimg.cn/large/006tNbRwly1gbepi1aegtj30ro0o6aat.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxkbce9j30ro0o676d.jpg)
这个时候代码就呼之欲出了。
......
......@@ -121,4 +121,4 @@ class Solution:
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludu08hcj30p00dwt9t.jpg)
......@@ -38,7 +38,7 @@ return num == 1;
我们先来看下,4 的幂次方用 2 进制表示是什么样的.
![263.342.power-of-four-1](../assets/problems/342.power-of-four-1.png)
![263.342.power-of-four-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua1uaopj30j009iwf1.jpg)
发现规律: 4 的幂次方的二进制表示 1 的位置都是在奇数位(且不在最低位),其他位置都为 0
......@@ -57,13 +57,13 @@ return num == 1;
`求与`, 如果等于本身,那么毫无疑问,这个 1 不再偶数位置,一定在奇数位置,因为如果在偶数位置,`求与`的结果就是 0 了
题目要求 n 是 32 位有符号整形,那么我们的特殊数字就应该是`01010101010101010101010101010101`(不用数了,一共 32 位)。
![263.342.power-of-four-2](../assets/problems/342.power-of-four-2.png)
![263.342.power-of-four-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua2pq5hj30fi0b0q41.jpg)
如上图,64和这个特殊数字求与,得到的是本身。 8 是 2的次方,但是不是4的次方,我们求与结果就是0了。
为了体现自己的逼格,我们可以使用计算器,来找一个逼格比较高的数字,这里我选了十六进制,结果是`0x55555555`
![263.342.power-of-four](../assets/problems/342.power-of-four.png)
![263.342.power-of-four](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua3mzibj30b20d70ua.jpg)
代码见下方代码区。
......
......@@ -62,10 +62,10 @@ Ok,下面来讲下`我是如何解这道题的`。
这道题抽象一下就是:
令:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1geu3kolxoyj305o03cwef.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludn2s3wj305o03cgle.jpg)
(图 1)
求:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1geu3jy6mxkj305o0360sp.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludny0u2j305o036wea.jpg)
(图 2)
## 第一直觉
......@@ -81,7 +81,7 @@ Ok,下面来讲下`我是如何解这道题的`。
用数学公式表示就是:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1geu3swzc9ej30co03yaa4.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludoulynj30co03ydfo.jpg)
(图 3)
截止目前,是一点点数学 + 一点点递归,我们继续往下看。现在问题是不是就很简单啦?直接翻译图三为代码即可,我们来看下这个时候的代码:
......@@ -98,7 +98,7 @@ class Solution:
毫无疑问,超时了。原因很简单,就是算法中包含了太多的重复计算。如果经常看我的题解的话,这句话应该不陌生。我随便截一个我之前讲过这个知识点的图。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1geu3zfz89jj313p0u0wnj.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludph6m5j313p0u00we.jpg)
(图 4)
> 原文链接:https://github.com/azl397985856/leetcode/blob/master/thinkings/dynamic-programming.md
......@@ -130,7 +130,7 @@ class Solution:
如图 4,我们的思考方式是从顶向下,这符合人们思考问题的方式。将其改造成如下图的自底向上方式就是动态规划。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1geu4a1grbvj31eq0r0wj8.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludra48hj31eq0r0gp1.jpg)
(图 5)
现在再来看下文章开头的代码:
......
......@@ -210,4 +210,4 @@ def GCD(a, b):
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzgattpj31bi0hcq5s.jpg)
......@@ -28,11 +28,11 @@ Output: 1
由于`异或``相同则位0,不同则位1`,因此我们可以把异或看成是一种不进位的加减法。
![371.sum-of-two-integers-1](../assets/problems/371.sum-of-two-integers-1.png)
![371.sum-of-two-integers-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud9y5phj30eu0b8jro.jpg)
由于`与``全部位1则位1,否则位0`,因此我们可以求与之后左移一位来表示进位。
![371.sum-of-two-integers-2](../assets/problems/371.sum-of-two-integers-2.png)
![371.sum-of-two-integers-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludauj6aj30ev0f00t5.jpg)
然后我们对上述两个元算结果递归求解即可。 递归的结束条件就是其中一个为0,我们直接返回另一个。
......
......@@ -41,18 +41,18 @@ You may assume k is always valid, 1 ≤ k ≤ n2.
最普通的二分法是有序数组中查找指定值(或者说满足某个条件的值)。由于是有序的,我们可以根据索引关系来确定大小关系,
因此这种思路比较直接,但是对于这道题目索引大小和数字大小没有直接的关系,因此这种二分思想就行不通了。
![378.kth-smallest-element-in-a-sorted-matrix-1](../assets/problems/378.kth-smallest-element-in-a-sorted-matrix-1.jpg)
![378.kth-smallest-element-in-a-sorted-matrix-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyc87pwj30gb03u0sx.jpg)
(普通的基于索引判断的二分法)
- 我们能够找到矩阵中最大的元素(右下角)和最小的元素(左上角)。我们可以求出值的中间,而不是上面那种普通二分法的索引的中间。
![378.kth-smallest-element-in-a-sorted-matrix-3](../assets/problems/378.kth-smallest-element-in-a-sorted-matrix-3.jpg)
![378.kth-smallest-element-in-a-sorted-matrix-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyd2629j30ch05faaa.jpg)
- 找到中间值之后,我们可以拿这个值去计算有多少元素是小于等于它的。
具体方式就是比较行的最后一列,如果中间值比最后一列大,说明中间元素肯定大于这一行的所有元素。 否则我们从后往前遍历直到不大于。
![378.kth-smallest-element-in-a-sorted-matrix-2](../assets/problems/378.kth-smallest-element-in-a-sorted-matrix-2.jpg)
![378.kth-smallest-element-in-a-sorted-matrix-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyeslbij30by06awep.jpg)
- 上一步我们会计算一个count,我们拿这个count和k进行比较
......@@ -66,7 +66,7 @@ You may assume k is always valid, 1 ≤ k ≤ n2.
整个计算过程是这样的:
![378.kth-smallest-element-in-a-sorted-matrix-4](../assets/problems/378.kth-smallest-element-in-a-sorted-matrix-4.jpg)
![378.kth-smallest-element-in-a-sorted-matrix-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyfm2okj30je0fq0uf.jpg)
这里有一个大家普遍都比较疑惑的点,也是我当初非常疑惑,困扰我很久的点, leetcode评论区也有很多人来问,就是“能够确保最终我们找到的元素一定在矩阵中么?”
......
......@@ -85,15 +85,15 @@ randomSet.getRandom();
以依次【1,2,3,4】之后为初始状态,那么此时状态是这样的:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjm9sg9olj30pg11wwiu.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7tf1suj30pg11w0ui.jpg)
而当要插入一个新的5的时候, 我们只需要分别向数组末尾和哈希表中插入这条记录即可。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjmanhni6j30ka126tdm.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7v71pqj30ka1260uv.jpg)
而删除的时候稍微有一点复杂:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjmbib4v5j30z60u049j.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7wyvclj30z60u040s.jpg)
## 关键点解析
......@@ -171,4 +171,4 @@ class RandomizedSet:
大家也可以关注我的公众号《力扣加加》获取更多更新鲜的LeetCode题解
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7yk9v4j30p00dwt9t.jpg)
......@@ -46,7 +46,7 @@ A solution set is:
我们先来看下通用解法的解题思路,我画了一张图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyrnqi82ej31190u0e81.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3y61r4j31190u0jw4.jpg)
> 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。
......
......@@ -43,7 +43,7 @@ nums2 = [3, 4]
首先了解一下 Median 的概念,一个数组中 median 就是把数组分成左右等分的中位数。
如下图:
![image.png](https://pic.leetcode-cn.com/100b34b378d0667969e7a4ca537c74e5103ce302731796740b3fa62b8bc55629-image.png)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyup7ixj310w0eote4.jpg)
这道题,很容易想到暴力解法,时间复杂度和空间复杂度都是`O(m+n)`, 不符合题中给出`O(log(m+n))`时间复杂度的要求。
我们可以从简单的解法入手,试了一下,暴力解法也是可以被 Leetcode Accept 的. 分析中会给出两种解法,暴力求解和二分解法。
......@@ -62,7 +62,7 @@ nums2 = [3, 4]
5. 如果`i`移动到`A`数组最后,那么直接把剩下的所有`B`依次放入新的数组中.
Merge 的过程如下图。
![image.png](https://pic.leetcode-cn.com/966c9a0fea7a5f433b82d660f82c5d8184a2ac73b8362d7be435aa0f63377a4c-image.png)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltywjka3j30sm13w4ba.jpg)
_时间复杂度: `O(m+n) - m is length of A, n is length of B`_
......@@ -78,13 +78,13 @@ _空间复杂度: `O(m+n)`_
对数组 A 的做 partition 的位置是区间`[0,m]`
如图:
![image.png](https://pic.leetcode-cn.com/816717da264c9e6970bfe0d696d9076febfe04b819f80a224cf2c73845d0f161-image.png)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyypek2j30o6166qmc.jpg)
下图给出几种不同情况的例子(注意但左边或者右边没有元素的时候,左边用`INF_MIN`,右边用`INF_MAX`表示左右的元素:
![image.png](https://pic.leetcode-cn.com/fe03bdcf4db4d74a0bebaa2907df3038c080e5915932fd581d06d4eb930b3035-image.png)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyzwjqej31bo0rq1it.jpg)
下图给出具体做的 partition 解题的例子步骤,
![image.png](https://pic.leetcode-cn.com/1c2093328c4edf06e416d0f43a94ed42b5a46ecc9f7ed72004b40b9fb47e12a4-image.png)
![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltz2832yj30u011g7ru.jpg)
_时间复杂度: `O(log(min(m, n)) - m is length of A, n is length of B`_
......@@ -285,4 +285,4 @@ class MedianSortedTwoArrayBinarySearch {
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzedtodj31bi0hcq5s.jpg)
......@@ -47,7 +47,7 @@ A solution set is:
我们先来看下通用解法的解题思路,我画了一张图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyrnqi82ej31190u0e81.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyjvckgj31190u0jw4.jpg)
> 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。
......
......@@ -6,7 +6,7 @@ https://leetcode.com/problems/trapping-rain-water/description/
> Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
![42.trapping-rain-water-1](../assets/problems/42.trapping-rain-water-1.png)
![42.trapping-rain-water-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2p6pzfj30bg04hmx3.jpg)
> The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
......@@ -225,5 +225,5 @@ For more solutions, visit my [LeetCode Solution Repo](https://github.com/azl3979
Follow my WeChat official account 力扣加加, which has lots of graphic solutions and teaches you how to recognize problem patterns to solve problems with efficiency.
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2wi41cj30p00dwt9t.jpg)
......@@ -13,7 +13,7 @@ The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In th
```
![42.trapping-rain-water-1](../assets/problems/42.trapping-rain-water-1.png)
![42.trapping-rain-water-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8i4s97j30bg04hmx3.jpg)
```
Example:
......@@ -243,4 +243,4 @@ public:
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8mu8kwj30p00dwt9t.jpg)
......@@ -94,11 +94,11 @@ var pathSum = function(root, sum) {
当我们执行到底部的时候:
![437.path-sum-iii](../assets/problems/437.path-sum-iii-1.jpg)
![437.path-sum-iii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludenaf3j30l60cyta7.jpg)
接着往上回溯:
![437.path-sum-iii-2](../assets/problems/437.path-sum-iii-2.jpg)
![437.path-sum-iii-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludf311tj30ii0bp0ty.jpg)
很容易看出,我们的hashmap不应该有第一张图的那个记录了,因此需要减去。
......
......@@ -34,7 +34,7 @@ My idea is to separate these four lists into two groups and combine them two by
As the picture shows:
![454.4-sum-ii](../assets/problems/454.4-sum-ii.png)
![454.4-sum-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5lpuodj30le0deab3.jpg)
Now that we got two `hashTable`, and the result would appear with some basic calculations.
......
......@@ -41,7 +41,7 @@ The two tuples are:
如图:
![454.4-sum-ii](../assets/problems/454.4-sum-ii.png)
![454.4-sum-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludk9vnxj30le0deab3.jpg)
这个时候我们得到了两个`hashTable`, 我们只需要进行简单的数学运算就可以得到结果。
......
......@@ -37,7 +37,7 @@ Output:
我们先来看下通用解法的解题思路,我画了一张图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyrnqi82ej31190u0e81.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty6q2cgj31190u0jw4.jpg)
> 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。
......
......@@ -56,7 +56,7 @@ cache.get(4); // returns 4
没有就新建 doublylinkedlist(head, tail), 把 node1 插入 doublylinkedlist head->next = node1.
如下图,
```
![460.lfu-cache-1](../assets/problems/460.lfu-cache-1.jpg)
![460.lfu-cache-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4z6wr1j314x0u0q61.jpg)
```
2. put(2, 2),
- 首先查找 nodeMap 中有没有 key=2 对应的 value,
......@@ -65,7 +65,7 @@ cache.get(4); // returns 4
没有就新建 doublylinkedlist(head, tail), 把 node2 插入 doublylinkedlist head->next = node2.
如下图,
```
![460.lfu-cache-2](../assets/problems/460.lfu-cache-2.jpg)
![460.lfu-cache-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4zqh7wj314k0u0adh.jpg)
```
3. get(1),
- 首先查找 nodeMap 中有没有 key=1 对应的 value,nodeMap:{[1, node1], [2, node2]},
......@@ -74,7 +74,7 @@ cache.get(4); // returns 4
- 更新 freqMap,插入 freq=2,node1
如下图,
```
![460.lfu-cache-3](../assets/problems/460.lfu-cache-3.jpg)
![460.lfu-cache-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu53jotrj313p0u0tdi.jpg)
```
4. put(3, 3),
- 判断 cache 的 capacity,已满,需要淘汰使用次数最少的元素,找到最小的 freq=1,删除双链表 tail node.prev
......@@ -85,7 +85,7 @@ cache.get(4); // returns 4
没有就新建 doublylinkedlist(head, tail), 把 node3 插入 doublylinkedlist head->next = node3.
如下图,
```
![460.lfu-cache-4](../assets/problems/460.lfu-cache-4.jpg)
![460.lfu-cache-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu54ffzrj313l0qwdkf.jpg)
```
5. get(2)
- 查找 nodeMap,如果没有对应的 key 的 value,返回 -1。
......@@ -97,7 +97,7 @@ cache.get(4); // returns 4
- 更新 freqMap,插入 freq=2,node3
如下图,
```
![460.lfu-cache-5](../assets/problems/460.lfu-cache-5.jpg)
![460.lfu-cache-5](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu54r66gj31380r1af4.jpg)
```
7. put(4, 4),
- 判断 cache 的 capacity,已满,需要淘汰使用次数最少的元素,找到最小的 freq=1,删除双链表 tail node.prev
......@@ -108,7 +108,7 @@ cache.get(4); // returns 4
没有就新建 doublylinkedlist(head, tail), 把 node4 插入 doublylinkedlist head->next = node4.
如下图,
```
![460.lfu-cache-6](../assets/problems/460.lfu-cache-6.jpg)
![460.lfu-cache-6](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu558d63j317s0trgrk.jpg)
```
8. get(1)
- 查找 nodeMap,如果没有对应的 key 的 value,返回 -1。
......@@ -120,7 +120,7 @@ cache.get(4); // returns 4
- 更新 freqMap,插入 freq=3,node3
如下图,
```
![460.lfu-cache-7](../assets/problems/460.lfu-cache-7.jpg)
![460.lfu-cache-7](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu56bzvhj313u0u0q7w.jpg)
```
10. get(4)
- 首先查找 nodeMap 中有没有 key=4 对应的 value,nodeMap:{[4, node4], [3, node3]},
......@@ -129,7 +129,7 @@ cache.get(4); // returns 4
- 更新 freqMap,插入 freq=2,node4
如下图,
```
![460.lfu-cache-8](../assets/problems/460.lfu-cache-8.jpg)
![460.lfu-cache-8](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu57axl0j314y0tc45n.jpg)
## 关键点分析
用两个`Map`分别保存 `nodeMap {key, node}``freqMap{frequent, DoublyLinkedList}`
......
......@@ -34,7 +34,7 @@ Output:
我们先来看下通用解法的解题思路,我画了一张图:
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyrnqi82ej31190u0e81.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6qwpr4j31190u0jw4.jpg)
> 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。
......
......@@ -43,7 +43,7 @@ https://leetcode-cn.com/problems/concatenated-words/
我们构造的前缀树大概是这样的:
![472.concatenated-words.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbkcox4r06j21eb15fgq8.jpg)
![472.concatenated-words.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaqn7pmj310g0u0wj5.jpg)
问题的关键在于第二步中的**查找每一个单词有几个单词表中的单词组成**。 其实如果你了解前缀树的话应该不难写出来。 比如查找 catsdogcats:
......
......@@ -112,7 +112,7 @@ DP formula:
For example:
![ones and zeros 2d dp](../assets/problems/474.ones-and-zeros-2d-dp.png)
![ones and zeros 2d dp](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxrhvyrj31400u0h2m.jpg)
####
- *Time Complexity:* `O(l * m * n) - l is strs length,m is number of 0,n number of 1`
......
......@@ -59,7 +59,7 @@ rotate the input matrix in-place such that it becomes:
通过观察发现,我们只需要将第 i 行变成第 n - i - 1 列, 因此我们只需要保存一个原有矩阵,然后按照这个规律一个个更新即可。
![48.rotate-image-1](../assets/problems/48.rotate-image-1.png)
![48.rotate-image-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty9mstdj30n0081q36.jpg)
代码:
......@@ -83,7 +83,7 @@ var rotate = function (matrix) {
事实上有一个更加巧妙的做法,我们可以巧妙地利用对称轴旋转达到我们的目的,如图,我们先进行一次以对角线为轴的翻转,然后
再进行一次以水平轴心线为轴的翻转即可。
![48.rotate-image-2](../assets/problems/48.rotate-image-2.png)
![48.rotate-image-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyaj6f1j30my0aegma.jpg)
这种做法的时间复杂度是 O(n^2) ,空间复杂度是 O(1)
......
......@@ -62,7 +62,7 @@ var groupAnagrams = function (strs) {
然后我们给每一个字符一个固定的数组下标,然后我们只需要更新每个字符出现的次数。 最后形成的 counts 数组如果一致,则说明他们可以通过
交换顺序得到。这种算法空间复杂度 O(n), 时间复杂度 O(n \* k), n 为数组长度,k 为字符串的平均长度.
![49.group-anagrams](../assets/problems/49.group-anagrams.png)
![49.group-anagrams](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubhv58qj30n209474l.jpg)
## 关键点解析
......
......@@ -36,13 +36,13 @@ Your output answer is guaranteed to be fitted in a 32-bit integer.
题目是给定一个数组,让你在数字前面添加 `+`或者`-`,使其和等于 target.
![494.target-sum](../assets/problems/494.target-sum.png)
![494.target-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzih10wj30is07ojrv.jpg)
暴力法的时间复杂度是指数级别的,因此我们不予考虑。我们需要换种思路.
我们将最终的结果分成两组,一组是我们添加了`+`的,一组是我们添加了`-`的。
![494.target-sum-2](../assets/problems/494.target-sum-2.png)
![494.target-sum-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzpo2ptj30mk05ijrp.jpg)
如上图,问题转化为如何求其中一组,我们不妨求前面标`+`的一组
......@@ -50,7 +50,7 @@ Your output answer is guaranteed to be fitted in a 32-bit integer.
通过进一步分析,我们得到了这样的关系:
![494.target-sum-3](../assets/problems/494.target-sum-3.png)
![494.target-sum-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzz1l61j30ks06xwfi.jpg)
因此问题转化为,求解`sumCount(nums, target)`,即 nums 数组中能够组成
target 的总数一共有多少种,这是一道我们之前做过的题目,使用动态规划可以解决。
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册