forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
luzhipeng
committed
May 3, 2019
1 parent
2d1371c
commit edf7916
Showing
11 changed files
with
272 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 页">7Vxbc6s2EP41fowH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yI8avkdHv8crrLU9uA74sNsaWST6pp+VyUkom8UKZH+RVjAjRLdGyGptbMOfBKfXBz+SyrNa8gJDT0ePBarl5HpGrhgQXSMToswdW5TSpVpGyTLKgqVagDrKgaXMxNyLwi7iVLykFFgntvd5QtlhnSEx02Kh4NKV2lFg9ZjYXSHjD9hB7rzrvIDaw5WDNpXGdqOE4za44DDvzbDlYSeJgmxnwDNvHVqSJmy7EAAmMBgKb9ckCT2SuVq4IiKv86QyLoe+nqAOc5NkPvOw0fWBrwgGkp/FbJ8J1xztYajjmLdaoguhbLnq+3S/mJzXCdLjw69Gmc0IXHqT+MWSpyPnvZjldSr/1+Np7M8Qf0wg9ExaqvEUskjU3oyuGCQT6rSd3ruxdTFhM6ur2ydFJZiFn3Yu7llKXOwaKoJ4jT99bpI+3w0jj7XO0l2ErtGZfPUMrmYPXY5BQh58zGZk97EdK5AwjSYp6wIj9HC3SvNXfwvxovI62bdlVeen+6xgEvNlqP9pq1Pvf56SdQxWU12p4+0OX4eqTk31IfjkY0d4uuSkoN56Lh0NMQSYSOw0+OadYNQB1Fn1TYqpH40761B5oEAlW59k6LxfF8UEqAtY/+1ND9LLpzdu9vLcxV2XTNwhj6eKCrEoAy3GpVDYW6iFtx2JGhWvrBYJdIFr9hkGUvfgiCzP4H</diagram></mxfile> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters