day2_长度最小的子数组(leetcode207)

给定一个含有 n 个正整数的数组和一个正整数 target 。

找出该数组中满足其总和大于等于 target 的长度最小的 连续

子数组

 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度如果不存在符合条件的子数组,返回 0 。

示例 1:

输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。

示例 2:

输入:target = 4, nums = [1,4,4]
输出:1

示例 3:

输入:target = 11, nums = [1,1,1,1,1,1,1,1]
输出:0

解释:

  1. 首先判断数组是否为空,如果为空则直接返回0。
  2. 定义两个指针startend,分别表示滑动窗口的起始位置和结束位置。同时定义变量sum用于记录当前滑动窗口内的元素和,以及变量minLen用于记录满足条件的最小子数组长度。
  3. 使用for循环遍历数组,每次将nums[end]加入到sum中。
  4. sum大于等于target时,进入内层的while循环。在循环内部:
    • 更新minLen为当前滑动窗口的长度end - start + 1minLen的较小值。
    • sum中减去nums[start],并将start指针向右移动,缩小滑动窗口的大小。
    • 重复这个过程,直到sum小于target为止。
  5. 最后,如果minLen的值没有被更新,说明不存在满足条件的子数组,返回0;否则返回minLen的值。

需要注意的点:

  1. 在计算子数组长度时,使用end - start + 1而不是end - start,因为数组的下标是从0开始的。
  2. 在更新minLen时,使用min(minLen, end - start + 1)来获取当前滑动窗口长度和之前的最小长度的较小值。
  3. 在返回结果时,使用三元运算符minLen == INT_MAX ? 0 : minLen来判断是否找到了满足条件的子数组。如果minLen的值没有被更新,说明不存在满足条件的子数组,返回0;否则返回minLen的值。

Explanation:

  1. First, check if the array is empty. If it is, return 0.
  2. Define two pointers, start and end, representing the start and end positions of the sliding window. Also, define a variable sum to keep track of the sum of elements within the current sliding window, and a variable minLen to store the minimum length of the subarray that satisfies the condition.
  3. Use a for loop to iterate through the array. In each iteration, add nums[end] to sum.
  4. When sum is greater than or equal to target, enter the inner while loop. Inside the loop:
    • Update minLen to be the smaller value between the current sliding window length end - start + 1 and minLen.
    • Subtract nums[start] from sum and move the start pointer to the right, shrinking the size of the sliding window.
    • Repeat this process until sum becomes less than target.
  5. Finally, if the value of minLen has not been updated, it means no subarray satisfies the condition, so return 0. Otherwise, return the value of minLen.

Points to note:

  1. When calculating the length of the subarray, use end - start + 1 instead of end - start because array indices start from 0.
  2. When updating minLen, use min(minLen, end - start + 1) to get the smaller value between the current sliding window length and the previous minimum length.
  3. When returning the result, use the ternary operator minLen == INT_MAX ? 0 : minLen to check if a subarray satisfying the condition was found. If the value of minLen has not been updated, it means no subarray satisfies the condition, so return 0. Otherwise, return the value of minLen.:

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        if (nums.size() == 0) return 0;
        int start = 0, sum = 0;
        int minLen = INT_MAX;
        for (int i = 0; i < nums.size(); i++) {
            sum += nums[i];
            while (target <= sum) {
                //cout << "yeah" << "target : " << target << "sum: " << sum<< endl;
                minLen = min(minLen, i - start);
                sum -= nums[start];
                start++;
            }
        }
        return minLen == INT_MAX ? 0 : minLen;
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值