Simple C solution using frequency counter


  • 0
    S
    bool checkSubarraySum(int* nums, int numsSize, int k) {
        int *kc;
        int i;
        int sum=0;
        
        if (k<0) k=-k;
        
        kc = (int *) malloc((k+1)*sizeof(int));
        if (kc == NULL) return(false);
        memset(kc, 0, (k+1)*sizeof(int));
        for(i=0;i<numsSize;i++) {
            sum = sum + nums[i];
            if (k!=0) {
                sum = sum%k;
            }
            if (kc[sum]) return(true);
            else kc[sum]++;
        }
        return(false);
    }
    

Log in to reply
 

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