560. Subarray Sum Equals K 523. Continuous Subarray Sum

The most intuitive idea is to traverse the array and add the numbers at the current position in turn. At the same time, use the array sum to record the sum of all the numbers before the current position.

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        //nums={1,2,1,2,1};
       // k=3;
       int len=nums.size();
        if(len<=0) return 0;
        if(len==1)
        {
            if(nums[0]==k) return 1;
            else return 0;
        }
        int sum
        int count=0;
       
       // sort(nums.begin(),nums.end());
        for(int i=0;i<=len-1;i++)
        {
            sum=nums[i];
            if(sum==k) count++;
            for(int j=i+1;j<=len-1;j++)
            {
                sum+=nums[j];
                if(sum==k) count++;
            }  
        }
        
       return count;
    }
};
Use sum to represent the sum of the numbers from the beginning of the array to the current position . We can also use Hash Table to store the number of sum occurrences. If there is a position where the sum is sum-k before the current position, the sum of the two positions The sum of the numbers in between is k, and the number of sub-arrays with the sum of k at the end of the current position is hash[sum-k], so the number of sub-arrays that satisfy the condition can be obtained by traversing the entire array. Specific code:
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        //nums={1,2,1,2,1};
       // k=3;
       int len=nums.size();
        if(len<=0) return 0;
        if(len==1)
        {
            if(nums[0]==k) return 1;
            else return 0;
        }
        int sum=0;
        int count=0;
        unordered_map<int,int> tmp;
        tmp[0]=1;//In order to satisfy the current number is K
        for(int i=0;i<=len-1;i++)
        {
            sum+=nums[i];
            count+=tmp[sum-k];
            tmp[sum]++;
        }
       return count;
    }
};

523

class Solution {//The simplest dynamic programming
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
       // nums={0,0};
       // k=0;
        unordered_map<int, int> hash;
        int sum = 0;
        
        hash[0] = 0;//The line hash[0] = -1 in the code is to facilitate the processing of edge cases
        for(int i=0; i<nums.size(); ++i) {
            sum += nums[i];
            if(k) sum %= k;
            if(hash.find(sum) != hash.end()) {
                if(i-hash[sum] > 1) return true;//The index of the first remainder is recorded, in order to make the length greater than or equal to 2
            }
            else hash[sum] = i;
        }
        
        return false;
    }
};

Violent method:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324759251&siteId=291194637
Recommended