> For the complete documentation index, see [llms.txt](https://851958789.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://851958789.gitbook.io/notes/0560_subarray_sum_equals_k/slt.md).

# 思路1 前缀和 + hash

一次遍历搞定

preSum\[i]定义为 nums\[0] + nums\[1] + ... + nums\[i]

那么preSum\[i] - preSum\[j-1]就是 nums\[j] + nums\[j+1] + ... + nums\[i-1] + nums\[i]

如果\[j, i]区间的和是k，那么就有preSum\[i] - preSum\[j-1] == k

-> preSum\[i] - k == preSum\[j-1]
