<div>public class Solution {
/*
* @param
*/
public int minSubArrayLen(int s, int[] nums) {
if(nums == null || nums.length == 1)
return 0;
int head = 0;
int tail = 0;
int sum = 0;
int result = nums.length;
boolean exists = false;
//Here's the main loop.
while(tail <= nums.length){
if(sum >= s){
exists = true;
if(tail - head == 1)
return 1;
result = Math.min(result, tail - head);
sum -= nums[head];
head++;
}else{
if(tail == nums.length)
break;
sum += nums[tail];
tail++;
}
}
if(exists)
return result;
else
return 0;
}
}
<pre name="code" class="java"><script src="https://code.youkuaiyun.com/snippets/1934952.js"></script>LeetCode209
最新推荐文章于 2025-04-30 20:32:23 发布