Share my 6ms c++ AC solution


  • 0
    D
    int minSubArrayLen(int s, vector<int>& nums) {
            int n=nums.size();
            int r=n+1;
            int sum=0;
            int i=0,j=0;
            while(j<n){
                while(sum<s && j<n)
                    sum += nums[j++];
                if(sum<s) break;
                else r=j-i<r?j-i:r;
                while(sum>=s)
                    sum -= nums[i++];
                r=j-i+1<r?j-i+1:r;
            }
            if(r==n+1) return 0;
            else return r;
    }

Log in to reply
 

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