mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 02:53:42 +00:00
Update 57.2 和为 S 的连续正数序列.md
补充滑动窗口解法的思路说明与执行示例,并完成样例输出的文字校对。
This commit is contained in:
@@ -10,11 +10,15 @@
|
||||
|
||||
```text
|
||||
[9, 10, 11, 12, 13, 14, 15, 16]
|
||||
[18, 19, 20, 21, 22]。
|
||||
[18, 19, 20, 21, 22]
|
||||
```
|
||||
|
||||
## 解题思路
|
||||
|
||||
用滑动窗口 start~end 表示当前的连续正数序列,curSum 为它们的和:和小于 target 时 end 右移扩大窗口,大于 target 时 start 右移缩小窗口,等于时记录结果,再把窗口整体右移一格继续查找。start 和 end 都只增不减,时间复杂度为 O(target)。
|
||||
|
||||
例如 target = 9 时,窗口依次为 1~2、1~3、1~4(和为 10)、2~4(和为 9,记录)、3~5(和为 12)、4~5(和为 9,记录)……end 到达 target 时结束,结果为 [2, 3, 4] 和 [4, 5]。
|
||||
|
||||
```java
|
||||
public int[][] fileCombination(int target) {
|
||||
List<int[]> ret = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user