Skip to content

Commit 74d918a

Browse files
authored
Update Readme.md
1 parent 188a295 commit 74d918a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### 930.Binary-Subarrays-With-Sum
22

3-
此题是考察对Hash+prefix的常见组合。
3+
#### 解法1:Hash+prefix
44

55
我们遍历每一个元素j,考察以j为结尾、满足条件的subarray,这样的起点i可以在哪里?如果满足条件的起点i有多种可能,那么答案就可以累加上这么多数量.
66

@@ -9,4 +9,11 @@
99
ret += Map[prefix[j] - S]
1010
```
1111

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+
1219
[Leetcode Link](https://leetcode.com/problems/binary-subarrays-with-sum)

0 commit comments

Comments
 (0)