Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed May 3, 2019
1 parent 2d1371c commit edf7916
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 16 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,25 @@ leetcode 题解,记录自己的 leetcode 解题之路。

![koko-eating-bananas](./assets/problems/koko-eating-bananas.png)

## 进度
## Top题目进度

- [Top 100 Linked Questions](https://leetcode.com/problemset/top-100-liked-questions/)(44/100)
- [Top 100 Linked Questions](https://leetcode.com/problemset/top-100-liked-questions/) (44 / 100)

- [Top Interview Questions](https://leetcode.com/problemset/top-interview-questions/)(59/145)
- [Top Interview Questions](https://leetcode.com/problemset/top-interview-questions/) (64 / 145)
## 传送门

### leetcode 经典题目的解析

> 这里仅列举具有代表性题目,并不是全部题目
#### 简单难度

- [0020.Valid Parentheses](./problems/20.validParentheses.md) 🖊
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md) 🆕
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
- [0136.single-number](./problems/136.single-number.md)
- [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)
- [0169.majority-element](./problems/169.majority-element.md) 🆕
- [0169.majority-element](./problems/169.majority-element.md)
- [0190.reverse-bits](./problems/190.reverse-bits.md)
- [0191.number-of-1-bits](./problems/191.number-of-1-bits.md)
- [0203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md)
Expand All @@ -101,12 +103,13 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
- [0015.3-sum](./problems/15.3-sum.md) 🆕
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
- [0039.combination-sum](./problems/39.combination-sum.md) 🆕
- [0040.combination-sum-ii](./problems/40.combination-sum-ii.md) 🆕
- [0046.permutations](./problems/46.permutations.md) 🆕
- [0047.permutations-ii](./problems/47.permutations-ii.md) 🆕
- [0039.combination-sum](./problems/39.combination-sum.md)
- [0040.combination-sum-ii](./problems/40.combination-sum-ii.md)
- [0046.permutations](./problems/46.permutations.md)
- [0047.permutations-ii](./problems/47.permutations-ii.md)
- [0055.jump-game](./problems/55.jump-game.md) 🆕
- [0062.unique-paths](./problems/62.unique-paths.md )🆕
- [0075.sort-colors](./problems/75.sort-colors.md)
Expand Down Expand Up @@ -134,6 +137,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [0416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md) 🆕
- [0518.coin-change-2](./problems/518.coin-change-2.md)
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
- [0877.stone-game](./problems/877.stone-game.md)
Expand Down Expand Up @@ -205,3 +209,9 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后
![wechat-group-chat](./assets/wechat-group-chat.jpeg)

(由于微信的限制,100个人以上只能邀请加入, 你可以添加我的机器人回复“leetcode”拉你进群)

## 贡献

- 如果有想法和创意,请提[issue](https://github.com/azl397985856/leetcode/issues)或者进群提
- 如果想贡献代码,请提[PR](https://github.com/azl397985856/leetcode/pulls)
- 如果需要修改项目中图片,[这里](./assets/drawio/)存放了项目中绘制图的源代码, 大家可以用[draw.io](https://www.draw.io/)打开进行编辑。
1 change: 1 addition & 0 deletions assets/drawio/15.3-sum.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-05-03T09:30:51.254Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="TW5ym4u-7gh4PydZElOX" version="10.6.6" type="device"><diagram id="b_2AcDZds6F21w0ZimBd" name="第 1 页">7ZpRb5swEMc/jaXtYREYHMhjoUn7sGqVsmnqXiYHnMAKGIHTpPv0M2CSgGnXbgk4UlIphbPB+Pc/uDsTYLjx9ibDaXBHfRIBqPlbYFwDCCe6zb8Lw3NlQLpWGVZZ6FcmfW+Yh7+JMNbd1qFP8kZHRmnEwrRp9GiSEI81bDjL6KbZbUmj5qgpXhHJMPdwJFu/hz4LKqsNrb39loSroB5ZH0+qlhjXncVM8gD7dHNgMqbAcDNKWbUVb10SFexqLtVxsxdadxeWkYS95YBfN3PrR/7w9Zt+n1t3X+4f5+btJziuTvOEo7WYsbha9lwjyOg68UlxFg0YziYIGZmn2CtaN1xzbgtYHPE9nW8uacKEilz6at+lEc3KcxnWTNNm/AodMSzJGNm+OCF9h4m7F6ExYdkz7yIOQAKs8CwDwRGqLJu9UmNb9AoOVTJFRyy8Y7U7+R4g3xAM38FTl3DWpn8FegRQ0GiTkjChDkq17eiQoARJG56RrRYjQ2I0vB8ZivmRKTH6z4fXMRgp5kdIxQeSqZgjyUGw9q0hISnmSdbRIS3DKDrIDZa2RzyP23OW0Udy0LKwkYm00wRD3dKG5WqreIe2w+HglCYqUmoHxMEp1VWTUqlVOyQOT0nO0of3pXZMHJ6SnKYPn161g+LwlOREPVnHuQSKT5k1aTSjXEIT0gqJwoSjcJXwXY8jItzuFABDD0dXoiEOfb8YphN/U6CXqvF21X4EpfRWeY5koawOoeDJhJKrhZzy+fncdlGsSzHdljPOfiWTixdJpZSGBeTpE59kXutUr7QVAH2cBzuafxMzwgsS3dM8ZCHtVPBzq8OCMkbjDokZTbs8gV9aWlx5vF0Vy6WjOPcwGfkkzYiHuTOOUprznj/LpUs5RdY0x53BfpfZ4AQ13QLKdzLs9ZH7hjXLi1uc2i1M5dziLAopU7M6Vqn7TVnOopZSAFTtvkpXUypwOod6SgVO51BRqcBJrqkuqfqrqboJu9599pqsQ7m+knS6ZGV9J+sG6rqbe83L4KWKU8Ax2um6Co4hv1FiGzpfxx8AcoqM1C2/+PBu8R8CxLvwoTTz47sjAKc5Lj8dr51Q8XeCWPCKstXnOMqilrBWVyjYvT3sJxbIFQaYWuDqGtgTMDWB4wDHKjeugD091dOAc7bhwig1V/2R4CNi++ZpkgVj3EwWupZizY4b33q/e/Dd/Q/ayraDXwUa0z8=</diagram></mxfile>
1 change: 1 addition & 0 deletions assets/drawio/454.4-sum-ii.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-05-03T11:25:45.542Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="1h5Ytb8GPu4hJzkI1NZY" version="10.6.6" type="device"><diagram id="hhZ2N8VzGzd5busGkVBM" name="第 1 页">7Vxbc6s2EP41fowHSVzMY3xrH047mclM2zxlOEaxaTGiICd2f30lI4xhFZucYINPyEMGLbp+30raXQkPyGS9/SXx4tVvzKfhABv+dkCmA4xdNBL/pWCXCUwbZYJlEviZ6EjwGPxHldBQ0k3g07SUkTMW8iAuCxcsiuiCl2RekrC3crYXFpZbjb0lBYLHhRdC6Z+Bz1eZdISdQv4rDZarvGVku9mbtZdnViNJV57P3o5EZDYgk4Qxnj2ttxMaSuxyXLJy83feHjqW0IjXKXD3NKMPf/+7cMnv0z+Ws+mz9e35DllZNa9euFEjVr3luxyChG0in8pajAEZv60CTh9jbyHfvgnOhWzF16FIIfH4wiKuWMSGSk9YyJJ9XcSZG8Zc9HCsmqUJp9t3B4QOMAn1omxNebITWfICRCGrVAu7eKiG81ZQhW0lWx3TZKqinlKP5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T99bpI+3w0jj7XO0l2ErtGZfPUMrmYPXY5BQh58zGZk97EdK5AwjSYp6wIj9HC3SvNXfwvxovI62bdlVeen+6xgEvNlqP9pq1Pvf56SdQxWU12p4+0OX4eqTk31IfjkY0d4uuSkoN56Lh0NMQSYSOw0+OadYNQB1Fn1TYqpH40761B5oEAlW59k6LxfF8UEqAtY/+1ND9LLpzdu9vLcxV2XTNwhj6eKCrEoAy3GpVDYW6iFtx2JGhWvrBYJdIFr9hkGUvfgiCzP4H</diagram></mxfile>
Binary file added assets/problems/15.3-sum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/454.4-sum-ii.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions problems/15.3-sum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
## 题目地址
https://leetcode.com/problems/3sum/description/

## 题目描述
```
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
```
## 思路

我们采用`分治`的思想. 想要找出三个数相加等于0,我们可以数组依次遍历,
每一项a[i]我们都认为它是最终能够用组成0中的一个数字,那么我们的目标就是找到
剩下的元素(除a[i]`两个`相加等于-a[i].

通过上面的思路,我们的问题转化为了`给定一个数组,找出其中两个相加等于给定值`
这个问题是比较简单的, 我们只需要对数组进行排序,然后双指针解决即可。 加上我们需要外层遍历依次数组,因此总的时间复杂度应该是O(N^2)。

思路如图所示:

![15.3-sum](../assets/problems/15.3-sum.png)

> 在这里之所以要排序解决是因为, 我们算法的瓶颈在这里不在于排序,而在于O(N^2),如果我们瓶颈是排序,就可以考虑别的方式了

> 如果找某一个特定元素,一个指针就够了。如果是找两个元素满足一定关系(比如求和等于特定值),需要双指针,
当然前提是数组有序。
## 关键点解析

- 排序之后,用双指针
- 分治

## 代码
```js

/*
* @lc app=leetcode id=15 lang=javascript
*
* [15] 3Sum
*
* https://leetcode.com/problems/3sum/description/
*
* algorithms
* Medium (23.51%)
* Total Accepted: 531.5K
* Total Submissions: 2.2M
* Testcase Example: '[-1,0,1,2,-1,-4]'
*
* Given an array nums of n integers, are there elements a, b, c in nums such
* that a + b + c = 0? Find all unique triplets in the array which gives the
* sum of zero.
*
* Note:
*
* The solution set must not contain duplicate triplets.
*
* Example:
*
*
* Given array nums = [-1, 0, 1, 2, -1, -4],
*
* A solution set is:
* [
* ⁠ [-1, 0, 1],
* ⁠ [-1, -1, 2]
* ]
*
*
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
if (nums.length < 3) return [];
const list = [];
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length; i++) {
// skip duplicated result without set
if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i;
let right = nums.length - 1;

// for each index i
// we want to find the triplet [i, left, right] which sum to 0
while (left < right) {
// skip i === left or i === right, in that case, the index i will be used twice
if (left === i) {
left++;
} else if (right === i) {
right--;
} else if (nums[left] + nums[right] + nums[i] === 0) {
list.push([nums[left], nums[right], nums[i]]);
// skip duplicated result without set
while(nums[left] === nums[left + 1]) {
left++;
}
left++;
// skip duplicated result without set
while(nums[right] === nums[right - 1]) {
right--;
}
right--;
continue;
} else if (nums[left] + nums[right] + nums[i] > 0) {
right--;
} else {
left++;
}
}
}
return list;
};
```
3 changes: 2 additions & 1 deletion problems/2.addTwoNumbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
https://leetcode.com/problems/add-two-numbers/description/

## 题目描述
```
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example
```
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Expand Down
2 changes: 1 addition & 1 deletion problems/236.lowest-common-ancestor-of-a-binary-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ var lowestCommonAncestor = function(root, p, q) {
```

## 扩展
如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是嗯呢么意思,对结果有什么样的影响?
如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是什么意思,对结果有什么样的影响?

116 changes: 116 additions & 0 deletions problems/454.4-sum-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@


## 题目地址
https://leetcode.com/problems/4sum-ii/description/

## 题目描述

```
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
Example:
Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
Output:
2
Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
```
## 思路

如果按照常规思路去完成查找需要四层遍历,时间复杂是O(n^4), 显然是行不通的。
因此我们有必要想一种更加高效的算法。

我一个思路就是我们将四个数组分成两组,两两结合。
然后我们分别计算`两两结合能够算出的和有哪些,以及其对应的个数`

如图:

![454.4-sum-ii](../assets/problems/454.4-sum-ii.png)


这个时候我们得到了两个`hashTable`, 我们只需要进行简单的数学运算就可以得到结果。

## 关键点解析



## 代码
```js

/*
* @lc app=leetcode id=454 lang=javascript
*
* [454] 4Sum II
*
* https://leetcode.com/problems/4sum-ii/description/
*
* algorithms
* Medium (49.93%)
* Total Accepted: 63.2K
* Total Submissions: 125.6K
* Testcase Example: '[1,2]\n[-2,-1]\n[-1,2]\n[0,2]'
*
* Given four lists A, B, C, D of integer values, compute how many tuples (i,
* j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
*
* To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤
* N ≤ 500. All integers are in the range of -2^28 to 2^28 - 1 and the result
* is guaranteed to be at most 2^31 - 1.
*
* Example:
*
*
* Input:
* A = [ 1, 2]
* B = [-2,-1]
* C = [-1, 2]
* D = [ 0, 2]
*
* Output:
* 2
*
* Explanation:
* The two tuples are:
* 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
* 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
*
*
*
*
*/
/**
* @param {number[]} A
* @param {number[]} B
* @param {number[]} C
* @param {number[]} D
* @return {number}
*/
var fourSumCount = function(A, B, C, D) {
const sumMapper = {};
let res = 0;
for (let i = 0; i < A.length; i++) {
for (let j = 0; j < B.length; j++) {
sumMapper[A[i] + B[j]] = (sumMapper[A[i] + B[j]] || 0) + 1;
}
}

for (let i = 0; i < C.length; i++) {
for (let j = 0; j < D.length; j++) {
res += sumMapper[- (C[i] + D[j])] || 0;
}
}

return res;
};
```
2 changes: 1 addition & 1 deletion thinkings/basic-data-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ return, children, sibling也都是一个fiber,因此fiber看起来就是一个

相关概念:

- 真二叉树
- 真二叉树 (所有节点的度数只能是偶数,即只能为0或者2)


另外我也专门开设了[二叉树的遍历](./binary-tree-traversal.md)章节, 具体细节和算法可以去那里查看。
Expand Down
8 changes: 4 additions & 4 deletions thinkings/dynamic-programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ f(n) = f(n-1) + f(n-2) 就是【状态转移公式】

### 相关问题

[硬币找零问题](../problems/322.coin-change.md)
[硬币找零问题2](../problems/518.coin-change-2.md)
[分词问题](../problems/139.word-break.md)
[416.partition-equal-subset-sum](../problems/416.partition-equal-subset-sum.md)
- [硬币找零问题](../problems/322.coin-change.md)
- [硬币找零问题2](../problems/518.coin-change-2.md)
- [分词问题](../problems/139.word-break.md)
- [416.partition-equal-subset-sum](../problems/416.partition-equal-subset-sum.md)

## 总结

Expand Down

0 comments on commit edf7916

Please sign in to comment.