[Continuous Subarray Sum] [C++] - Clean Solution


  • 1
    class Solution {
    public:
        bool checkSubarraySum(vector<int>& nums, int k) {
            map<int, int> sumto;
            for (int i = 0; i < nums.size(); i++) {
                sumto[i] = sumto[i - 1] + nums[i];
            }
    
            for (int i = 0; i < nums.size(); i++) {
                for (int j = i + 1; j < nums.size(); j++) {
                    if (k != 0 && (sumto[j] - sumto[i - 1]) % k == 0 || k == 0 && (sumto[j] - sumto[i - 1]) == 0) {
                        return true;
                    }
                }
            }
    
            return false;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.