代码随想录 Leetcode209.长度最小的子数组

题目:

代码随想录 Leetcode209.长度最小的子数组_第1张图片


代码(首刷看解析 2023年12月29日):

class Solution {
public:
    int minSubArrayLen( int target, vector& nums) {
        int slowIndex = 0,fastIndex = 0;
        int n = nums.size();
        int length = INT_MAX;
        int sum = 0;
        for(int fastIndex = 0; fastIndex < n; ++fastIndex){
            sum += nums[fastIndex];
            while(sum >= target){
                int nowLen = fastIndex - slowIndex + 1;
                length = length > nowLen ? nowLen : length;
                sum -= nums[slowIndex++]; 
            }
        }
        return length == INT_MAX? 0 : length;
    }
};

你可能感兴趣的:(#,leetcode,---medium,前端,算法,javascript)