Skip to content

Commit

Permalink
update source and index notes
Browse files Browse the repository at this point in the history
  • Loading branch information
billryan committed Feb 26, 2016
1 parent e03c146 commit c86e69e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions zh-hans/integer_array/subarray_sum_k.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

- GeeksforGeeks: [Find subarray with given sum - GeeksforGeeks](http://www.geeksforgeeks.org/find-subarray-with-given-sum/)

```
### Problem Statement

Given an nonnegative integer array, find a subarray where the sum of numbers is k.
Your code should return the index of the first number and the index of the last number.

Example
Given [1, 4, 20, 3, 10, 5], sum k = 33, return [2, 4].
```
#### Example

Given `[1, 4, 20, 3, 10, 5]`, sum k = 33, return `[2, 4]`.

## 题解1 - 哈希表

[Zero Sum Subarray | Data Structure and Algorithm](http://algorithm.yuanbin.me/zh-hans/integer_array/zero_sum_subarray.html) 的升级版,这道题求子串和为 K 的索引。首先我们可以考虑使用时间复杂度相对较低的哈希表解决。前一道题的核心约束条件为 $$f(i_1) - f(i_2) = 0$$,这道题则变为 $$f(i_1) - f(i_2) = k$$
[Zero Sum Subarray](http://algorithm.yuanbin.me/zh-hans/integer_array/zero_sum_subarray.html) 的升级版,这道题求子串和为 K 的索引。首先我们可以考虑使用时间复杂度相对较低的哈希表解决。前一道题的核心约束条件为 $$f(i_1) - f(i_2) = 0$$,这道题则变为 $$f(i_1) - f(i_2) = k$$, 那么相应的 index 则为 $$[i_1, i_2]$$.

### C++

Expand Down Expand Up @@ -85,7 +86,7 @@ int main(int argc, char *argv[])
### 复杂度分析
略,见 [Zero Sum Subarray | Data Structure and Algorithm](http://algorithm.yuanbin.me/zh-hans/integer_array/zero_sum_subarray.html)
略,见 [Zero Sum Subarray](http://algorithm.yuanbin.me/zh-hans/integer_array/zero_sum_subarray.html)
## 题解2 - 利用单调函数特性
Expand Down

0 comments on commit c86e69e

Please sign in to comment.