Algorithm-Sub-array whose sum is K

Algorithm-Sub-array whose sum is K

1. Sub-array whose sum is K

560. Sub-array whose sum is K

给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。

示例 1 :

输入:nums = [1,1,1], k = 2
输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。
说明 :

数组的长度为 [1, 20,000]。
数组中元素的范围是 [-1000, 1000] ,且整数 k 的范围是 [-1e7, 1e7]。

This question is a byte interview question, which is also a medium-level question on Leetcode. The purpose of the question is also obvious, which is to find the number of consecutive sequences whose sum is K. But in this question, the numbers may be out of order, and you can no longer do it with mathematics like the Jianzhi offer.

Of course, there is a violent solution to this problem, and the time complexity is O(N). The main idea is to declare two pointers, one pointing to the starting position of the continuous sequence, and the other pointing to the end of the continuous sequence. We can solve this problem by using two-level traversal. The method is relatively simple, but the time complexity is O(N^2). Although it can pass the test case, it takes a few hundred milliseconds. Changing to a language like python will definitely time out.

    public int subarraySum(int[] nums, int k) {
    
    
        int slow=0,quick=0;
        int tempK=k;
        int count=0;
        while(slow<nums.length){
    
    
            if(quick==nums.length){
    
    
                tempK=k;
                quick=++slow;
            }else if(tempK-nums[quick]==0){
    
    
                count++;
                tempK-=nums[quick++];   
            }else{
    
    
                tempK-=nums[quick++];   
            }
        }
        return count;
    }

We can use a hash table to achieve another solution. Remember the first question of Leetcode? For the two sum problem, there is an O(N) solution using HashMap. The idea of ​​this topic is consistent with it.

We use hashmap to store the number of occurrences of the same sum. Once the current sum is equal to the target sum, then the number of current eligible series +1. If sum-k is included, then the sum of m subsequences of sum-k and the current element can be Form a continuous sequence, so the current number of continuous subsequences must be added m. Finally, we want to record the number of occurrences of each sum.

    public int subarraySum(int[] nums, int k) {
    
    
        int sum=0,count=0;
        Map<Integer,Integer> map=new HashMap<>();
        for (int i=0;i<nums.length;i++){
    
    
            sum+=nums[i];
            if(sum==k){
    
    
                count++;
            }
            if(map.containsKey(sum-k)){
    
    
                count+=map.get(sum-k);
            }
            if(!map.containsKey(sum)){
    
    
                map.put(sum,1);
            }else {
    
    
                map.put(sum,map.get(sum)+1);
            }
        }
        return count;
    }

The code is written relatively clearly, but more interpretation.

Guess you like

Origin blog.csdn.net/qq_23594799/article/details/106005787