---
# System prepended metadata

title: 0523. Continuous Subarray Sum
tags: [Leetcode, FaceBook, Prefix Sum, Medium]

---

# 0523. Continuous Subarray Sum
###### tags: `Leetcode` `FaceBook` `Medium` `Prefix Sum`
Link: https://leetcode.com/problems/continuous-subarray-sum/
## 思路
凡是涉及到子序列求和的题一定要想到prefix sum
PrefixSum+HashSet
## Code
```java=
class Solution {
    public boolean checkSubarraySum(int[] nums, int k) {
        for(int i=1; i<nums.length;i++){
            nums[i]+=nums[i-1];
        }
        Set<Integer> set = new HashSet<>();
        set.add(0);
        for(int i=1; i<nums.length;i++){
            if(set.contains(nums[i]%k)){
                return true;
            }
            else{
                set.add(nums[i-1]%k);
            }
        }
        return false;
    }
}
```