Commit b6bdf8cc authored by JingmingZhang's avatar JingmingZhang
Browse files

Merge branch 'master' of github.com:zjming/LeetCodeAnimation

these are solved leetcode problem!
parents 5bf978cf 6db598b8
# LeetCode 第 75 号问题:颜色分类
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 75 号问题:颜色分类。题目难度为 Medium,目前通过率为 51.8% 。
### 题目描述
给定一个包含红色、白色和蓝色,一共 *n* 个元素的数组,**原地**对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
**注意:**
不能使用代码库中的排序函数来解决这道题。
**示例:**
```
输入: [2,0,2,1,1,0]
输出: [0,0,1,1,2,2]
```
**进阶:**
- 一个直观的解决方案是使用计数排序的两趟扫描算法。
首先,迭代计算出0、1 和 2 元素的个数,然后按照0、1、2的排序,重写当前数组。
- 你能想出一个仅使用常数空间的一趟扫描算法吗?
### 题目解析
结合三路快排 partition 思路的应用。
设定两个索引,一个从左往右滑动`zero`,一个从右往左滑动`two`
* 遍历`nums`,当`nums[i]`的值为1时,`i++`
*`nums[i]`的值为2时,`two`的值先减1,而后交换`nums[i]``nums[two]`,此时在观察`nums[i]`的值;
*`nums[i]`的值为0时,`zero++`,而后交换`nums[i]``nums[zero]``i++`;当 `i = two`时,结束循环。
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/6g5tm.gif)
### 代码实现
```
// 三路快速排序的思想
// 对整个数组只遍历了一遍
// 时间复杂度: O(n)
// 空间复杂度: O(1)
class Solution {
public:
void sortColors(vector<int> &nums) {
int zero = -1; // [0...zero] == 0
int two = nums.size(); // [two...n-1] == 2
for(int i = 0 ; i < two ; ){
if(nums[i] == 1){
i ++;
}else if (nums[i] == 2){
two--;
swap( nums[i] , nums[two]);
}else{ // nums[i] == 0
zero++;
swap(nums[zero] , nums[i]);
i++;
}
}
}
};
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 86 号问题:分割链表
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 86 号问题:分割链表。题目难度为 Easy,目前通过率为 47.8% 。
### 题目描述
给定一个链表和一个特定值 *x*,对链表进行分隔,使得所有小于 *x* 的节点都在大于或等于 *x* 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。
**示例:**
```
输入: head = 1->4->3->2->5->2, x = 3
输出: 1->2->2->4->3->5
```
### 题目解析
这道题要求我们划分链表,把所有小于给定值的节点都移到前面,大于该值的节点顺序不变,相当于一个局部排序的问题。
- 设定两个虚拟节点,`dummyHead1 `用来保存小于于该值的链表,`dummyHead2 `来保存大于等于该值的链表
- 遍历整个原始链表,将小于该值的放于`dummyHead1 `中,其余的放置在`dummyHead2 `
- 遍历结束后,将`dummyHead2 `插入到`dummyHead1 `后面
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/t96zg.gif)
### 代码实现
```
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* dummyHead1 = new ListNode(-1);
ListNode* dummyHead2 = new ListNode(-1);
ListNode* prev1 = dummyHead1;
ListNode* prev2 = dummyHead2;
for(ListNode* cur = head ; cur != NULL ;){
if(cur->val < x){
prev1->next = cur;
cur = cur->next;
prev1 = prev1->next;
prev1->next = NULL;
}
else{
prev2->next = cur;
cur = cur->next;
prev2 = prev2->next;
prev2->next = NULL;
}
}
prev1->next = dummyHead2->next;
ListNode* ret = dummyHead1->next;
delete dummyHead1;
delete dummyHead2;
return ret;
}
};
```
![](../../Pictures/qrcode.jpg)
# LeetCode 第 92 号问题:反转链表 II
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 92 号问题:反转链表 II。题目难度为 Medium,目前通过率为 43.8% 。
### 题目描述
反转从位置 *m**n* 的链表。请使用一趟扫描完成反转。
**说明:**
1 ≤ *m**n* ≤ 链表长度。
**示例:**
```
输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
```
### 题目解析
**[Reverse Linked List](https://xiaozhuanlan.com/topic/7513064892)**的延伸题。
可以考虑取出需要反转的这一小段链表,反转完后再插入到原先的链表中。
**以本题为例:**
变换的是 2,3,4这三个点,那么我们可以先取出 2 ,用 front 指针指向 2 ,然后当取出 3 的时候,我们把 3 加到 2 的前面,把 front 指针前移到 3 ,依次类推,到 4 后停止,这样我们得到一个新链表 4 -> 3 -> 2 , front 指针指向4。
对于原链表来说,**有两个点的位置很重要**,需要用指针记录下来,分别是 1 和 5 ,把新链表插入的时候需要这两个点的位置。
- 用 pre 指针记录 1 的位置
- 当 4 结点被取走后,5 的位置需要记下来
- 这样我们就可以把倒置后的那一小段链表加入到原链表中
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/rjjr0.gif)
### 代码实现
```
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *cur = dummy;
ListNode *pre, *front, *last;
for (int i = 1; i <= m - 1; ++i) cur = cur->next;
pre = cur;
last = cur->next;
for (int i = m; i <= n; ++i) {
cur = pre->next;
pre->next = cur->next;
cur->next = front;
front = cur;
}
cur = pre->next;
pre->next = front;
last->next = cur;
return dummy->next;
}
};
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 94 号问题:二叉树的中序遍历
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 94 号问题:二叉树的中序遍历。题目难度为 Medium,目前通过率为 35.8% 。
### 题目描述
给定一个二叉树,返回它的*中序* 遍历。
**示例:**
```
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
```
**进阶:** 递归算法很简单,你可以通过迭代算法完成吗?
### 题目解析
**栈(Stack)**的思路来处理问题。
中序遍历的顺序为**左-根-右**,具体算法为:
- 从根节点开始,先将根节点压入栈
- 然后再将其所有左子结点压入栈,取出栈顶节点,保存节点值
- 再将当前指针移到其右子节点上,若存在右子节点,则在下次循环时又可将其所有左子结点压入栈中
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/v17b8.gif)
### 代码实现
```
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 101 号问题:对称二叉树
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 第 101 号问题:对称二叉树。
### 题目描述
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 `[1,2,2,3,4,4,3]` 是对称的。
```
1
/ \
2 2
/ \ / \
3 4 4 3
```
### 题目解析
用递归做比较简单:一棵树是对称的**等价**于它的左子树和右子树两棵树是对称的,问题就转变为判断两棵树是否对称。
### 代码实现
```java
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
//把问题变成判断两棵树是否是对称的
return isSym(root.left, root.right);
}
//判断的是根节点为r1和r2的两棵树是否是对称的
public boolean isSym(TreeNode r1, TreeNode r2){
if(r1 == null && r2 == null) return true;
if(r1 == null || r2 == null) return false;
//这两棵树是对称需要满足的条件:
//1.俩根节点相等。 2.树1的左子树和树2的右子树,树2的左子树和树1的右子树都得是对称的
return r1.val == r2.val && isSym(r1.left, r2.right)
&& isSym(r1.right, r2.left);
}
}
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 102 号问题:二叉树的层序遍历
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 102 号问题:二叉树的层序遍历。题目难度为 Medium,目前通过率为 55.8% 。
### 题目描述
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
例如:
给定二叉树: `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回其层次遍历结果:
```
[
[3],
[9,20],
[15,7]
]
```
### 题目解析
该问题需要用到**队列**
- 建立一个queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 用for循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量存到二维向量里
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/2elr5.gif)
### 代码实现
```
/// BFS
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
queue<pair<TreeNode*,int>> q;
q.push(make_pair(root, 0));
while(!q.empty()){
TreeNode* node = q.front().first;
int level = q.front().second;
q.pop();
if(level == res.size())
res.push_back(vector<int>());
assert( level < res.size() );
res[level].push_back(node->val);
if(node->left)
q.push(make_pair(node->left, level + 1 ));
if(node->right)
q.push(make_pair(node->right, level + 1 ));
}
return res;
}
};
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 103 号问题:二叉树的锯齿形层次遍历
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 103 号问题:二叉树的锯齿形层次遍历。题目难度为 Medium,目前通过率为 43.8% 。
### 题目描述
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回锯齿形层次遍历如下:
```
[
[3],
[20,9],
[15,7]
]
```
### 题目解析
该问题需要用到**队列**,与之前的[二叉树的层次遍历](https://xiaozhuanlan.com/topic/8579460312)类似,不同点在于在偶数层需要翻转一下。
- 建立一个queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量存到二维向量里
- 如果该层为偶数层,则reverse翻转一下
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/xuoqo.gif)
### 代码实现
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/7mnmj.png)
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 107 号问题:二叉树的层次遍历 II
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 107 号问题:二叉树的层次遍历 II。题目难度为 Easy,目前通过率为 55.8% 。
### 题目描述
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回其自底向上的层次遍历为:
```
[
[15,7],
[9,20],
[3]
]
```
### 题目解析
该问题需要用到**队列**,解法与上篇[每天一算:Binary Tree Level Order Traversal](https://xiaozhuanlan.com/topic/8579460312)类似,区别在于最后存储方式的不同。
- 建立一个 queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 用 for 循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量**插入**到二维向量里
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/varp8.gif)
### 代码实现
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/9iccc.png)
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 110 号问题:平衡二叉树
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 110 号问题:平衡二叉树。
### 题目描述
给定一个二叉树,判断它是否是高度平衡的二叉树。
### 题目解析
采取**后序遍历**的方式遍历二叉树的每一个结点。
在遍历到一个结点之前已经遍历了它的左右子树,那么只要在遍历每个结点的时候记录它的深度(某一结点的深度等于它到叶结点的路径的长度),就可以一边遍历一边判断每个结点是不是平衡的。
### 动画描述
待补充
### 代码实现
```java
class Solution {
private boolean isBalanced = true;
public boolean isBalanced(TreeNode root) {
getDepth(root);
return isBalanced;
}
public int getDepth(TreeNode root) {
if (root == null)
return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
if (Math.abs(left - right) > 1) {
isBalanced = false;
}
return right > left ? right + 1 : left + 1;
}
}
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 125 号问题:验证回文串
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 第 125 号问题:验证回文串。这道题目是 **初级程序员** 在面试的时候经常遇到的一道算法题,而且面试官喜欢面试者手写!
### 题目描述
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
**说明:**本题中,我们将空字符串定义为有效的回文串。
**示例 1:**
```
输入: "A man, a plan, a canal: Panama"
输出: true
```
**示例 2:**
```
输入: "race a car"
输出: false
```
### 题目解析
先理解一个概念:所谓回文,就是一个正读和反读都一样的字符串。
先假设是验证单词 `level` 是否是回文字符串,通过概念涉及到 正 与 反 ,那么很容易想到使用双指针,从字符的开头和结尾处开始遍历整个字符串,相同则继续向前寻找,不同则直接返回 false。
而这里与单独验证一个单词是否是回文字符串有所区别的是加入了 空格 与 非字母数字的字符,但实际上的做法一样的:
一开始先建立两个指针,left 和 right , 让它们分别从字符的开头和结尾处开始遍历整个字符串。
如果遇到非字母数字的字符就跳过,继续往下找,直到找到下一个字母数字或者结束遍历,如果遇到大写字母,就将其转为小写。
当左右指针都找到字母数字时,可以进行比较的时候,比较这两个字符,如果相等,则两个指针向它们的前进方向挪动,然后继续比较下面两个分别找到的字母数字,若不相等,直接返回 false。
### 动画描述
![](../Animation/animation.gif)
### 代码实现
注:`isLetterOrDigit ` 方法确定指定的字符是否为字母或数字。
```java
class Solution {
public boolean isPalindrome(String s) {
if(s.length() == 0)
return true;
int l = 0, r = s.length() - 1;
while(l < r){
//确定指定的字符是否为字母或数字
if(!Character.isLetterOrDigit(s.charAt(l))){
l++;
}else if(!Character.isLetterOrDigit(s.charAt(r))){
r--;
}else{
if(Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r)))
return false;
l++;
r--;
}
}
return true;
}
}
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# 面试题53 - I. 在排序数组中查找数字 I
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上 面试题53 - I. 在排序数组中查找数字 I. 是算法入门的一道题。
## 题目
统计一个数字在排序数组中出现的次数。
 
示例 1:
```
输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
```
示例 2:
```
输入: nums = [5,7,7,8,8,10], target = 6
输出: 0
```
 
限制:
```
0 <= 数组长度 <= 50000
```
## 思路解析
### 暴力循环法
题目看上去是很简单,就是找到一个目标数字在数组中出现的次数,不管数组是有序还是无序的,我们都可以用的一种方法就是暴力循环法
#### 思路
定义一个count来记录目标值出现的次数,初始值为0,然后遍历这个数组,然后如果当前值和目标值target一致,那么count就加一,最后return count。这种解法的时间复杂度是O(N)
#### 代码实现
```javaScript
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let count = 0;
for(let i of nums) {
if (i === target) {
count++
}
}
return count
};
```
### 改良的暴力循环
#### 思路
因为数组已排序了,所以我们其实可以不用遍历全部,用双指针分别从头部和尾部开始同时遍历,然后找到目标值的左右边界的位置,然后通过计算得到count。其实就是比全部遍历少了目标值出现的次数,它的算法复杂度还是O(n)
count = 右边界的index - 左边界的index + 1
#### 代码实现
```javaScript
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let [left, right] = [0, nums.length - 1]
while(left <= right && (nums[left] !== target || nums[right] !== target)) {
if (left === right && nums[left] !== target) {
return 0;
}else if (nums[left] !== target) {
left++;
}else if (nums[right] !== target){
right--;
}
}
return right - left + 1;
};
```
### 二分法
#### 思路
除了遍历,我们在排序数组中查找值还可以用的一种方法是二分法,思路还是和改良的暴力循环法一样,先找到左右边界,然后做计算。时间复杂度为O(logn)
#### 代码实现
```javaScript
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let start = 0;
let mid = 0;
let end = nums.length - 1;
let left = 0;
let right = 0;
// 查找右边界
while(start <= end) {
mid = Math.ceil((start + end) / 2)
if (nums[mid] <= target) {
start = mid + 1
} else {
end = mid -1
}
}
right = start - 1; // 右边界
// 查找左边界
start = 0;
mid = 0;
end = nums.length - 1;
while(start <= end) {
mid = Math.ceil((start + end) / 2)
if (nums[mid] < target) {
start = mid + 1
} else {
end = mid -1
}
}
left = end + 1
return right - left + 1
};
```
## 动画理解
<video id="video" controls="" preload="none" >
<source id="mp4" src="../animation/Interview Question 53 - I. Find number in sort arrayI.mp4" type="video/mp4">
</video>
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
![LeetCode Animation All in One](https://upload-images.jianshu.io/upload_images/1940317-e837182a805cecce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
[![Travis](https://img.shields.io/badge/language-C++-red.svg)](https://developer.apple.com/)
[![Travis](https://img.shields.io/badge/language-Java-yellow.svg)](https://developer.apple.com/)
[这里有中文版本的README,点击它!](https://github.com/MisterBooo/LeetCodeAnimation/blob/master/Readme.md)
I will do my best to demonstrate all the questions on LeetCode in the form of animation.
I plan to take three to four years to complete it!
I look forward to witnessing this day with you!
The latest article published in WeChat **五分钟学算法** , you can pay attention to get the latest article.
## Problems
| ID | Problem | Article | Animation | Date|
| --- | --- | :--- |:--- | :--- |
| 000 |Ten Classic Sorting Algorithms | [十大经典排序算法动画与解析,看我就够了!(配代码完全版)](https://mp.weixin.qq.com/s/vn3KiV-ez79FmbZ36SX9lg) | ![归并排序.gif](https://upload-images.jianshu.io/upload_images/1940317-92f62b62af03e233.gif?imageMogr2/auto-orient/strip)|
| 001 |Two-Sum | [每天一算:Two Sum](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483740&idx=1&sn=1950545589ea9b86ee65fbb6be1f4290&chksm=fa0e6eddcd79e7cb542b7d4dc1304eead516994315fa4f52b575230f0f022c9e0a88ede3714e&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161033.gif) |
| 002 |Add Two Numbers| [图解LeetCode第 2 号问题:两个数字相加](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484231&idx=2&sn=6a9eb4fd0619c822e4dede69b8d841c8&chksm=fa0e6cc6cd79e5d0c03fffcd65b665fed62db9dca9c97771898f388ea292ce806bfd6eb908b5&token=934487935&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092831.gif)|
| 003 |Longest Substring Without Repeating Characters| [图解LeetCode第 3 号问题:无重复字符的最长子串](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484265&idx=2&sn=7f72afb341865923315bd51e1f50beff&chksm=fa0e6ce8cd79e5fe4be925fd5f01f59f59010c6c965fb3daefac79992593a6e58990c212e0bb&token=1412967663&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092855.gif)|
| 019| Remove-Nth-Node-From-End-of-List |[每天一算:Remove Nth Node From End of List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483821&idx=1&sn=11ecccab76cd53163e9dedb75effeb93&chksm=fa0e6e2ccd79e73ae9137c0d91b3533df4ea4ead4ad081834b8d91ff364c0d55c350ddcfa6c4&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161058.gif)|
| 020|Valid-Parentheses | [每天一算:Valid Parentheses](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483824&idx=1&sn=ab9362e125dc5e2b3ef1611cad9448c2&chksm=fa0e6e31cd79e727c6e1e0e3c467e193edb6ae841a41e5dc8eef39d0bf3141cc53f63b019cba&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161106.gif)|
| 024| Swap-Nodes-in-Pairs | [每天一算:Swap Nodes in Pairs](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483773&idx=1&sn=db6cf272df968cd6571eb0bb50ecc721&chksm=fa0e6efccd79e7ea26810d335e6ece9ac23b8e3ac31be00dbd534018737ccb3ef9a00f22aff3&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161115.gif)|
| 026| Remove-Duplicates-from-Sorted-Array| [图解LeetCode第 26 号问题:删除排序数组中的重复项](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484284&idx=2&sn=c8af62a82a62a21217d0f0b2b5891e4f&chksm=fa0e6cfdcd79e5ebe8726a61f93b834467d29b7d9e60a44feb990388f9e98605ac1e3f7e723d&token=762342620&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161124.gif)|
| 075| Sort-Colors| [每天一算:Sort Colors](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483706&idx=1&sn=905f43c882a91b55fd169d812620f277&chksm=fa0e6ebbcd79e7ad8857b0dad9ad14dbaf17fe557ef56ba600cec26b2bb668df2e171431d74c&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161136.gif)|
| 086| Partition-List| [每天一算:Partition List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483781&idx=1&sn=f31548ebbb2cf9ba56d979d3e51ddde2&chksm=fa0e6e04cd79e712d6cc7ff8e8b7631b7300ac0fa1a3e4c4e3b836de7a01fb5d0d6428a18bc4&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161147.gif)|
|092 |Reverse-Linked-List-II | [每天一算:Reverse Linked List II](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483820&idx=1&sn=113e87b55c8ac8e22e9db00673798118&chksm=fa0e6e2dcd79e73b5835a262599b935783de3317a453bc0ed8df9fa5d1532785a085ea663e59&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161156.gif)|
| 094|Binary-Tree-Inorder-Traversal | [每天一算:Binary Tree Inorder Traversal ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483853&idx=1&sn=94cd4b4ee8dc2268290a72334c6af57b&chksm=fa0e6e4ccd79e75a41a6b78397b80cdfccda332823874475b516f997f89e786488599fc5cc1e&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161208.gif)|
|102 |Binary-Tree-Level-Order-Traversal| [每天一算:Binary Tree Level Order Traveral](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483868&idx=1&sn=d50041789fcd13a75a2296f620b69d71&chksm=fa0e6e5dcd79e74b0030ac5129f10ec4ba87c98da63c5904affe9f06e06ecf28695c410d3ec7&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161232.gif)|
|103 |Binary Tree Zigzag Level Order Traversal| [图解LeetCode第 103 号问题:二叉树的锯齿形层次遍历](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484290&idx=2&sn=c29c4eefcbe8954cca6b3c8491ebccf1&chksm=fa0e6c03cd79e515581905322a3a22a3f3d10d24ca668a9d5aaef00932f0237eeaeaf3199668&token=1840661183&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092922.gif)|
|107 | Binary Tree Level Order Traversal II | 每天一算: Binary Tree Level Order Traversal II|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092949.gif)|
|136 | Single Number | [一道让你拍案叫绝的算法题 ](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484505&idx=1&sn=4c1c056dd4852c3b4b1ead51c90a9b2d&chksm=fa0e6bd8cd79e2ce8188dcdd8843a5d071248906bfb8971c62d513dbd69b816acc191a78e4b2&token=487128715&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190116110804.gif)| 2019-01-16 |
|144 | Binary-Tree-Preorder-Traversal| [每天一算:Binary Tree Preorder Traversal](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483843&idx=1&sn=994bf0d42dd9941a879a3a3ed500a4d6&chksm=fa0e6e42cd79e75472404eb5da7ee98f20d303efe230eb4f41efec57164630f555e7111e62ff&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181207112441.gif)|
| 145| Binary-Tree-Postorder-Traversal | [每天一算:Binary Tree Postorder Traversal](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483853&idx=1&sn=94cd4b4ee8dc2268290a72334c6af57b&chksm=fa0e6e4ccd79e75a41a6b78397b80cdfccda332823874475b516f997f89e786488599fc5cc1e&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181219084940.gif)|
| 146| LRU Cache | LRU缓存机制 |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190125143756.gif)| 2019-01-25 Made by Jun chen|
| 150| Evaluate-Reverse-Polish-Notation | [每天一算:Evaluate Reverse Polish Notation](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483834&idx=1&sn=27cbff99f10dfcdb56cb37c237d7f2bb&chksm=fa0e6e3bcd79e72dc430bf81aed9dde9bd01634239dcf7820d6befa881efd323d9d58d76d90d&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161304.gif)|
| 167| Two-Sum-II-Input-array-is-sorted | [每天一算:Two Sum II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483711&idx=1&sn=3afec74e9e9effa71dc0b22659e14b44&chksm=fa0e6ebecd79e7a84db7861c9b5dbccdc98aa9d9a6994dda49a37edeb729e8242ea6af8f20ad&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161314.gif)|
|199 | Binary Tree Right Side View | 每天一算:Binary Tree Right Side View |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161328.gif)|
| 203| Remove-Linked-List-Elements | [每天一算:Remove Linked List Elements](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483766&idx=1&sn=6721376a65680bf7cf9064cf7b1ae4ae&chksm=fa0e6ef7cd79e7e1665e60fe6ea3f2087bca518c1573bc4c4b9425573f98401bafc59542dca0&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161338.gif)|
|206 | Reverse Linked List | [每天一算: Reverse Linked List ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483799&idx=1&sn=c2212c8963809e8d3392abeeb851dbfc&chksm=fa0e6e16cd79e7003c2d30b1a2bb4f23dc56df38e3efedd0ab2cfae291609280a832eabe67de&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210093009.gif)|
|209 | Minimum Size Subarray Sum | 每天一算: Minimum Size Subarray Sum |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210093031.gif)|
| 219|Contains-Duplicate-II |[每天一算:Contains Duplicate II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483755&idx=1&sn=2501b6ca09c43eaa9fba71a9bd1f5253&chksm=fa0e6eeacd79e7fc192c0a23cf90d98fe6f2c35f9e4f2d0f937ccba45a58cf23a0a9c49d35d5&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161352.gif)|
|237| Delete-Node-in-a-Linked-List |每天一算:Delete Node in a Linked List|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161400.gif) |
|279| Perfect Squares |[图解LeetCode第 279 号问题: 完全平方数](https://mp.weixin.qq.com/s/53AlHe29fJF1hEwj0cj7ZA)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190119213317.gif) | Made by 王琛 2019-01-19日|
|283 |Move-Zeroes |[每天一算:Move Zeros](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483700&idx=1&sn=465f778d60e8560742feab5844d7cac5&chksm=fa0e6eb5cd79e7a357899d378edb532b498cd63e3ce9113f8ac74d397ce4b214ca5aa8198b7d&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161421.gif)|
|328 |Odd-Even-Linked-List | [每天一算:Odd Even Linked List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483786&idx=1&sn=f7810950b34675e1c4420361faf5e361&chksm=fa0e6e0bcd79e71d2c6fc6a4a68b6ef7a17abc3dc9897548f8e44b51e9494f52c4cebbc4176e&scene=21#wechat_redirect) |![](https://diycode.b0.upaiyun.com/photo/2018/94e5c38540029690c93314b3d697caaf.gif)|
|344 | Reverse-String |每天一算:Reverse String |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181211110918.gif)|
|349 | Intersection-of-Two-Arrays| [每天一算:Intersection of Two Arrays ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483726&idx=1&sn=a887f6b983058d97c183dd300832ecbb&chksm=fa0e6ecfcd79e7d985587b543622c85aadc83a4d7a074135e1356fb4a0ebfd07e7af13467906&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161441.gif)|
| 350| Intersection-of-Two-Arrays-II| [每天一算:Intersection of Two Arrays II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483733&idx=1&sn=946bd6de3251437dd77b43ecab056c82&chksm=fa0e6ed4cd79e7c2a439b5f1853bf5154a3438ed282c7ba5e94948780c426a1f1492c0b201c4&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161729.gif)|
|445| Add Two Numbers II |[图解LeetCode第 445 号问题: 两数相加 II](https://mp.weixin.qq.com/s/z8_1dK7mw9gxfhhSZUBVgg)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190119213714.gif) | Made by 王琛 2019-01-19日|
|447 | Number-of-Boomerangs | [每日一算:Number of Boomerangs](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483747&idx=1&sn=7774eee0b252b311257134f6a52c4e2d&chksm=fa0e6ee2cd79e7f44858c46c3d04859ced9073dbb9de95ce7ee0bcc131e613862ddfd9a6f158&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161738.gif)|
|454 |4Sum-II | 每日一算:4Sum II|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161751.gif)|
|642 |Design Search Autocomplete System | [图解 LeetCode 第 642 号问题:搜索自动完成系统](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484491&idx=1&sn=b329d90370d183b5a58bbf03f6a436ae&chksm=fa0e6bcacd79e2dc05bb5eaabd888561b82c37700b511e4971aa76ec42a630c0a35ef3e4721b&token=397665543&lang=zh_CN#rd)|Made by Jun [Click here](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484491&idx=1&sn=b329d90370d183b5a58bbf03f6a436ae&chksm=fa0e6bcacd79e2dc05bb5eaabd888561b82c37700b511e4971aa76ec42a630c0a35ef3e4721b&token=397665543&lang=zh_CN#rd)|
If the link of the article cannot be clicked, it means that the article has not been published. Please look forward to it :)
## Code source
This warehouse code unless otherwise specified, all from this warehouse
[Play-Leetcode](https://github.com/liuyubobobo/Play-Leetcode)
## Supplement
The warehouse is kept up to date.
2018-12-29 explain:
[《2019年LeetCodeAnimationd的更新计划》](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484375&idx=1&sn=5a5482d9863342650d8b43bb59171f7c&chksm=fa0e6c56cd79e540115e52500b80c8e72001c87ddceb7c0ae1de166fd283d632b960cde41aca&token=578760218&lang=zh_CN#rd)
2018-12-07 explain:
In order to better perform LeetCode animation, i am working hard to learn more data structures and algorithms.
I am currently writing an article 《动画图解数据结构》, and will continue to update this warehouse after finishing the series of articles on animation graphic data structure.
E-mail:misterbigbooo@gmail.com
like it! star❤️ it!
![LeetCode Animation All in One](https://upload-images.jianshu.io/upload_images/1940317-e837182a805cecce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
[![Travis](https://img.shields.io/badge/language-C++-red.svg)](https://developer.apple.com/.md)
[![Travis](https://img.shields.io/badge/language-Java-yellow.svg)](https://developer.apple.com/.md)
[There is an English version of README here. just click it!](https://github.com/MisterBooo/LeetCodeAnimation/blob/master/README-En.md)
我会尽力将 LeetCode 上所有的题目都用动画的形式演示出来,计划用 3 到 4 年时间去完成它,期待与你见证这一天!
文章最新首发于微信公众号 **图解面试算法**,您可以关注获取最新的文章。
![](Pictures/qrcode.jpg)
文章同步博客地址:https://www.algomooc.com
## 汇总
| 序号 | 题目&题解 | 动画 |
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| 0 | [十大经典排序算法动画与解析,看我就够了!(配代码完全版)](https://mp.weixin.qq.com/s/vn3KiV-ez79FmbZ36SX9lg) | |
| 1 | [两数之和](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第1号问题:两数之和.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/av47v.gif) |
| 2 | [两数相加](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第2号问题:两数相加.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz933.gif) |
| 3 | [无重复字符的最长子串](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第3号问题:无重复字符的最长子串.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/vxa7f.gif) |
| 4 | [寻找两个有序数组的中位数](https://mp.weixin.qq.com/s/FBlH7o-ssj_iMEPLcvsY2w) | |
| 9 | [回文数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第9号问题:回文数.md) | |
| 10 | [正则表达式匹配](https://mp.weixin.qq.com/s/ZoytuPt5dfP5pMODbuKnCQ) | |
| 11 | [盛最多水的容器](https://mp.weixin.qq.com/s/0PCW-7JzU8rfrLA5GQmFiQ) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/en8u4.gif) |
| 15 | [三数之和](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第15号问题:三数之和.md) | |
| 19 | [删除链表的倒数第 N 个节点](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第19号问题:删除链表的倒数第N个节点.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/metqn.gif) |
| 20 | [有效的括号](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第20号问题:有效的括号.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/ey3lr.gif) |
| 21 | [合并两个有序链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第21号问题:合并两个有序链表.md) | |
| 23 | [合并 K 个排序链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第23号问题:合并K个排序链表.md) | |
| 24 | [两两交换链表中的节点](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第24号问题:两两交换链表中的节点.md) | |
| 25 | [K 个一组翻转链表](https://mp.weixin.qq.com/s/YOz66mJchVIEQjA7TBV2cg) | |
| 26 | [删除排序数组中的重复项](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第26号问题:删除排序数组中的重复项.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/4tk72.gif) |
| 32 | [最长有效括号](https://mp.weixin.qq.com/s/CPIDKHsg3ROT10rVFDyDJQ) | |
| 38 | [报数](https://mp.weixin.qq.com/s/DKXJR8pNX3fKGvtSn0TEjw) | |
| 41 | [缺失的第一个正数](<https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247486473&idx=3&sn=06bf5f9c17cd3cb328934acc698b0ec3&chksm=fa0e6388cd79ea9e30c67dd2e5251056d860ba45e1fcbbcd4524573072305396d06629312c91&token=1735603091&lang=zh_CN#rd>) | |
| 66 | [加一](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第66号问题:加一.md) | |
| 75 | [颜色分类](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第75号问题:颜色分类.md) | ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/ehgxu.gif) |
| 86 | [分割链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第86号问题:分割链表.md) | |
| 92 | [反转链表 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第92号问题:反转链表II.md) | |
| 94 | [二叉树的中序遍历](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第94号问题:二叉树的中序遍历.md) | |
| 101 | [对称二叉树](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第101号问题:对称二叉树.md) | |
| 102 | [二叉树的层序遍历](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第102号问题:二叉树的层序遍历.md) | |
| 103 | [二叉树的锯齿形层次遍历](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第103号问题:二叉树的锯齿形层次遍历.md) | |
| 107 | [二叉树的层次遍历 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第107号问题:二叉树的层次遍历II.md) | |
| 118 | [杨辉三角](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第118号问题:杨辉三角.md) | |
| 119 | [杨辉三角II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第119号问题:杨辉三角II.md) | |
| 110 | [平衡二叉树](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第110号问题:平衡二叉树.md) | |
| 121 | [买卖股票的最佳时机](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第121号问题:买卖股票的最佳时机.md) | |
| 122 | [买卖股票的最佳时机II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第122号问题:买卖股票的最佳时机II.md) | |
| 123 | [买卖股票的最佳时机III](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第123号问题:买卖股票的最佳时机III.md) | |
| 125 | [验证回文串](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第125号问题:验证回文串.md) | |
| 131 | [分割回文串](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第131号问题:分割回文串.md) | |
| 136 | [只出现一次的数字](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第136号问题:只出现一次的数字.md) | |
| 138 | [复制带随机指针](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第138号问题:复制带随机指针.md) | |
| 139 | [单词拆分](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第139号问题:单词拆分.md) | |
| 141 | [环形链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第141号问题:环形链表.md) | |
| 144 | [二叉树的前序遍历](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第144号问题:二叉树的前序遍历.md) | |
| 145 | [二叉树的后序遍历](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第145号问题:二叉树的后序遍历.md) | |
| 146 | [LRU缓存机制](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第146号问题:LRU缓存机制.md) | |
| 150 | [逆波兰表达式求值](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第150号问题:逆波兰表达式求值.md) | |
| 153 | [寻找旋转排序数组中的最小值](<https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247486243&idx=3&sn=ed49d65662be56ca76753141b91ce6ef&chksm=fa0e64a2cd79edb4b64d11e3af5f0ee2f597bbd843d5f34a812ef70f0f97e190d381e77f05f8&token=387372133&lang=zh_CN#rd>) | |
| 164 | [最大间距](https://mp.weixin.qq.com/s/xHxjCDdFZyCW2pnY6Cz8SQ) | |
| 167 | [两数之和 II - 输入有序数组](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第167号问题:两数之和II-输入有序数组.md) | |
| 169 | [求众数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第169号问题:求众数.md) | |
| 172 | [阶乘后的零](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第172号问题:阶乘后的零.md) | |
| 187 | [重复的 DNA 序列](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第187号问题:重复的DNA序列.md) | |
| 191 | [位1的个数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第191号问题:位1的个数.md) | |
| 199 | [二叉树的右视图](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第199号问题:二叉树的右视图.md) | |
| 201 | [数字范围按位与](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第201号问题:数字范围按位与.md) | |
| 203 | [移除链表元素](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第203号问题:移除链表元素.md) | |
| 206 | [反转链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第206号问题:反转链表.md) | |
| 209 | [长度最小的子数组](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第209号问题:长度最小的子数组.md) | |
| 219 | [存在重复元素 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第219号问题:存在重复元素II.md) | |
| 229 | [求众数II](https://mp.weixin.qq.com/s/ObO4eQbjp1s1g_WXPkjixQ) | |
| 231 | [2的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第231号问题:2的幂.md) | |
| 232 | [使用栈实现队列](https://mp.weixin.qq.com/s/j6w94_PjvsL9Dip_xBcqcg) | |
| 237 | [删除链表中的节点](https://mp.weixin.qq.com/s/2XdUeDNblryFpXpTUgsaMQ) | |
| 239 | [滑动窗口最大值](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第239号问题:滑动窗口最大值.md) | |
| 242 | [有效的字母异位词](https://mp.weixin.qq.com/s/o5HTxmOgpftSaQdebS9zyQ) | |
| 268 | [缺失数字](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第268号问题:缺失数字.md) | |
| 279 | [完全平方数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第279号问题:完全平方数.md) | |
| 283 | [移动零](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第283号问题:移动零.md) | |
| 295 | [数据流的中位数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第295号问题:数据流的中位数.md) | |
| 301 | [删除无效的括号](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第301号问题:删除无效的括号.md) | |
| 319 | [灯泡开关](https://mp.weixin.qq.com/s/u35RGvT5Bc2o7jM-Uu_ZYA) | |
| 326 | [3 的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第326号问题:3的幂.md) | |
| 328 | [奇偶链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第328号问题:奇偶链表.md) | |
| 342 | [4的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第342号问题:4的幂.md) | |
| 344 | [反转字符串](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第344号问题:反转字符串.md) | |
| 347 | [前K个高频元素](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第347号问题:前K个高频元素.md) | |
| 349 | [两个数组的交集](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第349号问题:两个数组的交集.md) | |
| 350 | [两个数组的交集 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第350号问题:两个数组的交集II.md) | |
| 445 | [两数相加 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第445号问题:两数相加II.md) | |
| 447 | [回旋镖的数量](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第447号问题:回旋镖的数量.md) | |
| 454 | [四数相加 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第454号问题:四数相加II.md) | |
| 642 | [设计一个搜索自动完成系统](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第642号问题:设计一个搜索自动完成系统.md) | |
| 690 | [员工的重要性](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第690号问题:员工的重要性.md) | |
| 739 | [每日温度](https://mp.weixin.qq.com/s/3kDSOHyd-qOw7apzj0Z9YQ) | |
| 877 | [石子游戏](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第877号问题:石子游戏.md) | |
| 1025 | [除数博弈](https://mp.weixin.qq.com/s/0u6z02QYj1OpAwf54k8-Dw) | |
| 1099 | [小于 K 的两数之和](https://mp.weixin.qq.com/s/S6BbLeP_th_9JheNX7NN-w) | |
![](Pictures/qrcode.jpg)
# LeetCode 第 101 号问题:对称二叉树
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
题目来源于 LeetCode 第 101 号问题:对称二叉树。
### 题目描述
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 `[1,2,2,3,4,4,3]` 是对称的。
```
1
/ \
2 2
/ \ / \
3 4 4 3
```
### 题目解析
用递归做比较简单:一棵树是对称的**等价**于它的左子树和右子树两棵树是对称的,问题就转变为判断两棵树是否对称。
### 代码实现
```java
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
//把问题变成判断两棵树是否是对称的
return isSym(root.left, root.right);
}
//判断的是根节点为r1和r2的两棵树是否是对称的
public boolean isSym(TreeNode r1, TreeNode r2){
if(r1 == null && r2 == null) return true;
if(r1 == null || r2 == null) return false;
//这两棵树是对称需要满足的条件:
//1.俩根节点相等。 2.树1的左子树和树2的右子树,树2的左子树和树1的右子树都得是对称的
return r1.val == r2.val && isSym(r1.left, r2.right)
&& isSym(r1.right, r2.left);
}
}
```
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/kbvfp.gif)
\ No newline at end of file
# LeetCode 第 102 号问题:二叉树的层序遍历
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
题目来源于 LeetCode 上第 102 号问题:二叉树的层序遍历。题目难度为 Medium,目前通过率为 55.8% 。
### 题目描述
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
例如:
给定二叉树: `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回其层次遍历结果:
```
[
[3],
[9,20],
[15,7]
]
```
### 题目解析
该问题需要用到**队列**
- 建立一个queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 用for循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量存到二维向量里
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/2elr5.gif)
### 代码实现
```
/// BFS
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
queue<pair<TreeNode*,int>> q;
q.push(make_pair(root, 0));
while(!q.empty()){
TreeNode* node = q.front().first;
int level = q.front().second;
q.pop();
if(level == res.size())
res.push_back(vector<int>());
assert( level < res.size() );
res[level].push_back(node->val);
if(node->left)
q.push(make_pair(node->left, level + 1 ));
if(node->right)
q.push(make_pair(node->right, level + 1 ));
}
return res;
}
};
```
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/96yrg.png)
\ No newline at end of file
# LeetCode 第 103 号问题:二叉树的锯齿形层次遍历
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
题目来源于 LeetCode 上第 103 号问题:二叉树的锯齿形层次遍历。题目难度为 Medium,目前通过率为 43.8% 。
### 题目描述
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回锯齿形层次遍历如下:
```
[
[3],
[20,9],
[15,7]
]
```
### 题目解析
该问题需要用到**队列**,与之前的[二叉树的层次遍历](https://xiaozhuanlan.com/topic/8579460312)类似,不同点在于在偶数层需要翻转一下。
- 建立一个queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量存到二维向量里
- 如果该层为偶数层,则reverse翻转一下
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/xuoqo.gif)
### 代码实现
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/7mnmj.png)
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/q9yt7.png)
\ No newline at end of file
# LeetCode 第 107 号问题:二叉树的层次遍历 II
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
题目来源于 LeetCode 上第 107 号问题:二叉树的层次遍历 II。题目难度为 Easy,目前通过率为 55.8% 。
### 题目描述
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回其自底向上的层次遍历为:
```
[
[15,7],
[9,20],
[3]
]
```
### 题目解析
该问题需要用到**队列**,解法与上篇[每天一算:Binary Tree Level Order Traversal](https://xiaozhuanlan.com/topic/8579460312)类似,区别在于最后存储方式的不同。
- 建立一个 queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 用 for 循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量**插入**到二维向量里
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/varp8.gif)
### 代码实现
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/9iccc.png)
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/tdqxb.png)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment