There was an error while loading. Please reload this page.
1 parent 188a295 commit 74d918aCopy full SHA for 74d918a
Hash/930.Binary-Subarrays-With-Sum/Readme.md
@@ -1,6 +1,6 @@
1
### 930.Binary-Subarrays-With-Sum
2
3
-此题是考察对Hash+prefix的常见组合。
+#### 解法1:Hash+prefix
4
5
我们遍历每一个元素j,考察以j为结尾、满足条件的subarray,这样的起点i可以在哪里?如果满足条件的起点i有多种可能,那么答案就可以累加上这么多数量.
6
@@ -9,4 +9,11 @@
9
ret += Map[prefix[j] - S]
10
```
11
12
+#### 解法2:Sliding Window
13
+遍历左边界左边界。假设左边界为i,那么可以向右单调移动j直至滑窗内的元素和恰好为S。此时如果知道j右边有k个连续的0,那么就意味着以i为左边界、元素和是S的滑窗就有k+1个。
14
+
15
+对于每个元素,它后面有多少个连续的0,可以提前预处理得到的。
16
17
+因为i和j都是单调移动的,所以时间复杂度是o(N).
18
19
[Leetcode Link](https://leetcode.com/problems/binary-subarrays-with-sum)
0 commit comments