标签:fas lang 代码 策略 com http math 指针 pre
1. 长度最小的子数组 20210629晚
来源链接:leetCode:209
确定快慢指针移动的策略
var minSubArrayLen = function(target, nums) {
let slow = 0;
let fast = 0;
let sum = 0;
let minLength = 0;
while(fast < nums.length){// 1. 当快指针滑出数组停止[快指针的运动策略]
sum += nums[fast];
if(sum >= target){// 2. 当和大于等于目标值时,进行慢指针的移动[重点][慢指针的运动策略]
while(sum >= target){
sum -= nums[slow];
// 3. 更新最小长度
minLength = minLength == 0 ? (fast - slow + 1) : Math.min(minLength,(fast-slow+1));
slow++;
}
}
fast++;
}
return minLength;
};
标签:fas lang 代码 策略 com http math 指针 pre
原文地址:https://www.cnblogs.com/Neilisme/p/14952580.html