leetcode 209. Minimum length subarray (high-quality solution)

Code:

//时间复杂度 O(N) ,空间复杂度 O(1)
class Solution {
    //采用滑动窗口的方法解决
    public int minSubArrayLen(int target, int[] nums) {
        int numsLength=nums.length;
        int minLength=Integer.MAX_VALUE;
        int left=0;
        int right=0;
        int sum=0;

        while (right<numsLength){
            sum+=nums[right];
            while (sum>=target){
                //sum 符合要求
                //以当前 left 指针指向的数的最小子数组已经找到
                minLength=Math.min(minLength,right-left+1);
                sum-=nums[left++];
            }
            //sum 不符合要求
            right++;
        }

        if(minLength==Integer.MAX_VALUE){
            minLength=0;
        }
        return minLength;
    }
}

answer:

        We can use the sliding window method to solve this problem. For subarray and string problems, the sliding window method is often used.

        If we want to get the subarray with the smallest length, we can first think of a brute force method, which is to get all the subarrays in the array, and then traverse to find the subarray that meets the conditions and has the smallest length. The sliding window solution is based on This idea is implemented, but optimized

        We take example 1 in the question as an example, input: target = 7, nums = [2,3,1,2,4,3]

        1. First, we use two pointers L and R to point to the position with subscript 0 for traversal (their two traversal rules are different). Now between the L and R pointers is the subarray we want to discuss at this time. , we define a sum variable to represent the length of the subarray at this time, sum+= nums[ R ], at this time the sum of the subarrays is 2, which is less than target, so we need to expand the subarray

2        3        1        2        4        3

L

R

        2. After moving the R pointer back by one position, the sum of the sub-array is 3 more than before, sum+= nums[ R ] =5 < target, so we still need to move the R pointer back until the sub-array until the sum of

2        3        1        2        4        3

L

          R

        3. When the R pointer moves to the following position, the sum of the subarrays between the L pointer and the R pointer is 2+3+1+2 > target, which meets the conditions, so the variable minLength and the current subarray are taken. The minimum value of the array length is saved. Since this is the first minimum length obtained, minLength records the length at this time 4. There is a detail here. After recording the length at this time, we will Do you need to keep the R pointer moving to the right? The answer is no, because we have obtained the subarray with the smallest length that starts with the data pointed to by the L pointer and meets the conditions. When we let R continue to move to the right, since nums is a An array of positive integers, so the obtained subarray must meet the conditions, but the length is increased by 1, and what we want is the minimum subarray length. Increasing the length by 1 here is completely unnecessary, so it is definitely not what we want. Answers obtained

2        3        1        2        4        3

L

                              R

        4. We have already obtained the minimum subarray length starting with the current L pointer, so we can move the L pointer to the right and discuss the minimum subarray length starting with the next data, sum -= nums[ L ] = 6, here There is a detail, do we need to let the R pointer return to the position of the L pointer to traverse? The answer is no, because when the R pointer points to the current position and the data pointed before the L pointer is added, it meets the requirements. Therefore, when the L pointer moves, the R pointer must be at least at the current position to meet the requirements. At this time, sum = 6 < ; target , so the R pointer needs to be moved to the right to expand the sum of the subarrays

2        3        1        2        4        3

          L

                              R

        5. The next step is the loop operation. When the R pointer points to the position of nums.length, the loop ends

おすすめ

転載: blog.csdn.net/q322359/article/details/134750936