C++ Accepted 4ms O(n)


  • 0
    L
    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int start=0, end=0;
            int total=nums.size();
            int sum=0;
            int minlength=total+1;
            while(start<total){
                while((sum<s)&&(end<total)){
                    sum+=nums[end++];
                }
                if((end==total)&&(sum<s))  break;
                int currentlength=end-start;
                if(minlength>currentlength){
                    minlength=currentlength;
                }
                sum-=nums[start++];
            }
            if(minlength>total){
                return 0;
            }else   return minlength;
        }
    };
    

Log in to reply
 

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