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
Jun 28, 2019
1 parent
2dc3d94
commit 3b66227
Showing
9 changed files
with
248 additions
and
2 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
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-06-28T08:13:53.640Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" etag="cftZSqvnzNecOcGb3rIg" version="10.8.2" type="device"><diagram id="h5GrOjgCf6RgwEEO3gBQ" name="第 1 页">7ZxNl6I4FIZ/jcvykARCWFofPb3pOXWmFtPdOwZSyhkUG2Or8+s7CAGSWEhR6tU+s6kiNyGQl4ck9yY4Ig/z7R95uJx9yWKejrATb0fkcYRxgJj8Wxh2pcFDTmmY5klcmlBjeEn+45VRFVsnMV9pBUWWpSJZ6sYoWyx4JDRbmOfZRi/2mqX6VZfhlFuGlyhMbevfSSxmpZVhv7F/5sl0pq6MaFDmzENVuGrJahbG2aZlIk8j8pBnmSiP5tsHnhbaKV3K8z69kVvfWM4Xos8J6V+7++9x/MRnn54n8aPzZfnn5K6q5WeYrg3hV2KnNJD1SLll4n4zSwR/WYZRkbORD1zaZmKeyhSSh1VlPBd8++ZdorrtkhmezbnId7JIdQJR1694YVVy04jvVaZZS3dlC6v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9C3xZWS0qHUcTdi+OZ6DJhqKJutEkoGge2tdFU1F4C9m+FQ2j9Mc6Uxl3q/0X5BNZQFKwbTJHxSfiypFojMTnIeVO20SnxX+X+ONlKGZ3q/X8LkkSdXXZmvIGymLW+yL4VuhviO7FLLIFN1yeyhSmyXRRvGaS3ILv+yK8mkRhOqky5kkcp2+Fc/NsvYh5rL0WJ119cw98oFh/nX2Z74YP7c/6n4kLMoHNPsI/HxMy2fw2QtmtND8wQZ5+AQ==</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-06-28T08:34:51.832Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" etag="KifvGym_ZoCTUZblt02z" version="10.8.2" type="device"><diagram id="xFxd8wiRep7FchxX0ach" name="第 1 页">5Vtbc6M2FP41fowHISDwmE3SbR863TY73e5Th4BimAByhby2++srgrjp4NTrAIJsHjLoAAK+852r5BW+TQ8fmb+NfqUhSVamER5W+G5lmh5yxf9CcCwFNjJKwYbFYSlCjeAh/pdIYXXZLg5J3rmQU5rweNsVBjTLSMA7Mp8xuu9e9kST7lO3/oYAwUPgJ1D6JQ55VEpd87qR/0ziTVQ9GTleeSb1q4vll+SRH9J9S4TvV/iWUcrLo/RwS5ICuwqX8r6fTpytX4yRjJ9zw+ffHr2/P/8Sbn//au6//PnxMQ3/uJKzfPOTXfeDc36sIGB0l4WkmMVY4Q/7KObkYesHxdm90LmQRTxNxAiJw6c4SW5pQtnLvfjJDUgQCHnOGX0mrTOPrm3ZxYTyDQjj5HDy01ANmCAaoSnh7CguqW5wJMaSZJYc7huN2VIUtZRVyXzJkU09cQOjOJBIfgeqJkDVXCCqtQ3OBFUMUMVL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H</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.
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,159 @@ | ||
## 题目地址 | ||
|
||
https://leetcode.com/problems/path-sum-iii/description/ | ||
|
||
## 题目描述 | ||
|
||
``` | ||
You are given a binary tree in which each node contains an integer value. | ||
Find the number of paths that sum to a given value. | ||
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes). | ||
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000. | ||
Example: | ||
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 | ||
10 | ||
/ \ | ||
5 -3 | ||
/ \ \ | ||
3 2 11 | ||
/ \ \ | ||
3 -2 1 | ||
Return 3. The paths that sum to 8 are: | ||
1. 5 -> 3 | ||
2. 5 -> 2 -> 1 | ||
3. -3 -> 11 | ||
``` | ||
|
||
## 思路 | ||
这道题目是要我们求解出任何一个节点出发到子孙节点的路径中和为指定值。 | ||
注意这里,不一定是从根节点出发,也不一定在叶子节点结束。 | ||
|
||
一种简单的思路就是直接递归解决,空间复杂度O(n) 时间复杂度介于O(nlogn) 和 O(n^2), | ||
具体代码: | ||
|
||
```js | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
// the number of the paths starting from self | ||
function helper(root, sum) { | ||
if (root === null) return 0; | ||
const l = helper(root.left, sum - root.val); | ||
const r = helper(root.right, sum - root.val); | ||
|
||
return l + r + (root.val === sum ? 1 : 0); | ||
} | ||
/** | ||
* @param {TreeNode} root | ||
* @param {number} sum | ||
* @return {number} | ||
*/ | ||
var pathSum = function(root, sum) { | ||
// 空间复杂度O(n) 时间复杂度介于O(nlogn) 和 O(n^2) | ||
// tag: dfs tree | ||
if (root === null) return 0; | ||
// the number of the paths starting from self | ||
const self = helper(root, sum); | ||
// we don't know the answer, so we just pass it down | ||
const l = pathSum(root.left, sum); | ||
// we don't know the answer, so we just pass it down | ||
const r = pathSum(root.right, sum); | ||
|
||
return self + l + r; | ||
}; | ||
|
||
``` | ||
|
||
|
||
但是还有一种空间复杂度更加优秀的算法,利用hashmap来避免重复计算,时间复杂度和空间复杂度都是O(n)。 | ||
这种思路是`subarray-sum-equals-k`的升级版本,如果那道题目你可以O(n)解决,这道题目难度就不会很大, | ||
只是将数组换成了二叉树。关于具体的思路可以看[这道题目](./560.subarray-sum-equals-k.md) | ||
|
||
|
||
这里有一个不一样的地方,这里我说明一下,就是为什么要有`hashmap[acc] = hashmap[acc] - 1;`, | ||
原因很简单,就是我们DFS的时候,从底部往上回溯的时候,map的值应该也回溯。如果你对回溯法比较熟悉的话, | ||
应该很容易理解,如果不熟悉可以参考[这道题目](./46.permutations.md), 这道题目就是通过`tempList.pop()`来完成的。 | ||
|
||
另外我画了一个图,相信看完你就明白了。 | ||
|
||
当我们执行到底部的时候: | ||
|
||
![437.path-sum-iii](../assets/problems/437.path-sum-iii-1.jpg) | ||
|
||
接着往上回溯: | ||
|
||
![437.path-sum-iii-2](../assets/problems/437.path-sum-iii-2.jpg) | ||
|
||
很容易看出,我们的hashmap不应该有第一张图的那个记录了,因此需要减去。 | ||
|
||
|
||
具体实现见下方代码区。 | ||
|
||
## 关键点解析 | ||
|
||
- 通过hashmap,以时间换空间 | ||
- 对于这种连续的元素求和问题,有一个共同的思路,可以参考[这道题目](./560.subarray-sum-equals-k.md) | ||
|
||
## 代码 | ||
|
||
* 语言支持:JS | ||
|
||
```js | ||
|
||
|
||
/* | ||
* @lc app=leetcode id=437 lang=javascript | ||
* | ||
* [437] Path Sum III | ||
*/ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
function helper(root, acc, target, hashmap) { | ||
// see also : https://leetcode.com/problems/subarray-sum-equals-k/ | ||
|
||
if (root === null) return 0; | ||
let count = 0; | ||
acc += root.val; | ||
if (acc === target) count++; | ||
if (hashmap[acc - target] !== void 0) { | ||
count += hashmap[acc - target]; | ||
} | ||
if (hashmap[acc] === void 0) { | ||
hashmap[acc] = 1; | ||
} else { | ||
hashmap[acc] += 1; | ||
} | ||
const res = | ||
count + | ||
helper(root.left, acc, target, hashmap) + | ||
helper(root.right, acc, target, hashmap); | ||
|
||
// 这里要注意别忘记了 | ||
hashmap[acc] = hashmap[acc] - 1; | ||
|
||
return res; | ||
} | ||
|
||
var pathSum = function(root, sum) { | ||
// 时间复杂度和空间复杂度都是O(n) | ||
const hashmap = {}; | ||
return helper(root, 0, sum, hashmap); | ||
}; | ||
``` |
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,83 @@ | ||
|
||
|
||
## 题目地址 | ||
https://leetcode.com/problems/subarray-sum-equals-k/description/ | ||
|
||
## 题目描述 | ||
``` | ||
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. | ||
Example 1: | ||
Input:nums = [1,1,1], k = 2 | ||
Output: 2 | ||
Note: | ||
The length of the array is in range [1, 20,000]. | ||
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. | ||
``` | ||
## 思路 | ||
符合直觉的做法是暴力求解所有的子数组,然后分别计算和,如果等于k,count就+1.这种做法的时间复杂度为O(n^2). | ||
|
||
这里有一种更加巧妙的方法,我们可以借助额外的空间,使用hashmap来简化时间复杂度,这种算法的时间复杂度可以达到O(n). | ||
|
||
我们维护一个hashmap,hashmap的key为累加值acc,value为累加值acc出现的次数。 | ||
我们迭代数组,然后不断更新acc和hashmap,如果acc 等于k,那么很明显应该+1. 如果hashmap[acc - k] 存在, | ||
我们就把它加到结果中去即可。 | ||
|
||
语言比较难以解释,我画了一个图来演示nums = [1,2,3,3,0,3,4,2], k = 6的情况。 | ||
|
||
![560.subarray-sum-equals-k](../assets/problems/560.subarray-sum-equals-k.jpg) | ||
|
||
如图,当访问到nums[3]的时候,hashmap如图所示,这个时候count为2. | ||
其中之一是[1,2,3],这个好理解。还有一个是[3,3]. | ||
|
||
这个[3,3]正是我们通过hashmap[acc - k]即hashmap[9 - 6]得到的。 | ||
|
||
## 关键点解析 | ||
|
||
- 可以利用hashmap记录和的累加值来避免重复计算 | ||
|
||
## 代码 | ||
```js | ||
/* | ||
* @lc app=leetcode id=560 lang=javascript | ||
* | ||
* [560] Subarray Sum Equals K | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var subarraySum = function(nums, k) { | ||
const hashmap = {}; | ||
let acc = 0; | ||
let count = 0; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
acc += nums[i]; | ||
|
||
if (acc === k) count++; | ||
|
||
if (hashmap[acc - k] !== void 0) { | ||
count += hashmap[acc - k]; | ||
} | ||
|
||
if (hashmap[acc] === void 0) { | ||
hashmap[acc] = 1; | ||
} else { | ||
hashmap[acc] += 1; | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
``` | ||
|
||
## 扩展 | ||
|
||
这是一道类似的题目,但是会稍微复杂一点, 题目地址: [437.path-sum-iii](./437.path-sum-iii.md) | ||
|
||
|
||
|
||
|