523. Continuous Subarray Sum

Posted The Tech Road

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了523. Continuous Subarray Sum相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        unordered_map<int,int> m;   // 从头元素开始的sum,取模为key的index。
        m[0] = -1;
        int _sum = 0;
        for (int i = 0; i < nums.size(); i++) {
            _sum += nums[i];
            if (k != 0)  
                _sum = _sum % k;
            if (m.find(_sum) != m.end()) {
                if (m[_sum]+1 < i)
                    return true;
            }
            else {
                m[_sum] = i;
            }
        }
        return false;
    }
};

 

以上是关于523. Continuous Subarray Sum的主要内容,如果未能解决你的问题,请参考以下文章

523. Continuous Subarray Sum

leetcode 523. Continuous Subarray Sum

Leetcode 523: Continuous Subarray Sum

leetcode 523. Continuous Subarray Sum

LeetCode 523: Continuous Subarray Sum

523. Continuous Subarray Sum