Accepted C Rt O(n) Solution


  • 0
    R
    int minSubArrayLen(int s, int* nums, int numsSize) {
    	if(0==numsSize) return 0;
    
    	int minlen=numsSize+1;
    	int p1=0;
    	int p2=0;
    	int sum=0;
    	do {
    		sum+=nums[p2];
    		++p2;
    		while(sum>=s){
    			int tl=p2-p1;
    			minlen= minlen<tl? minlen : tl;
    
    			sum-=nums[p1];
    			++p1;
    		}
    	}while(p2<numsSize);
    	return minlen==numsSize+1? 0 : minlen;
    }
    

    just scan the array, adding the right ones and subtracting left ones.u will find the shortest or nothing


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.