c++ two pointers


  • 0
    A
    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            const int sz=nums.size();
            if(sz==0) return 0;
            
            int sm=nums[0];
            int i=0,j=1;
            int res=INT_MAX;
            
            while(j<=sz&&i<=j){
                if(sm>=s){
                    res=min(res,j-i);
                }
                if(sm>=s){
                    sm-=nums[i++];
                }else{
                    sm+=nums[j++];
                }
                
            }
            return res==INT_MAX?0:res;
        }
    };
    

Log in to reply
 

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