Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

209. Minimum Size Subarray Sum

Posted on 2020-06-01 | Edited on 2021-01-22

https://leetcode.com/problems/minimum-size-subarray-sum/

two pointers
Explanation-solution-(two-pointers))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums==null||nums.length==0) return 0;
int l=0,r=0;
int minLen=Integer.MAX_VALUE;
int sum=0;
while(r<nums.length){
sum+=nums[r++];
while(sum>=s){
minLen=r-l<minLen?r-l:minLen;
sum-=nums[l++];
}
}
return minLen==Integer.MAX_VALUE?0:minLen;
}
}
/*
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums==null||nums.length==0) return 0;
int l=0,r=0;
int minLen=Integer.MAX_VALUE;
int sum=0;
while(l<=r&&r<=nums.length){
if(sum<s&&r==nums.length) break;
if(sum<s){
sum+=nums[r];
r++;
}
if(sum>=s){
minLen=r-l<minLen?r-l:minLen;
sum-=nums[l];
l++;
}
}
return minLen==Integer.MAX_VALUE?0:minLen;
}
}*/

<1…185186187…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%