Short C++ Solution


  • 0
    F

    Increase count of 1_counter and check to update max_consecutive_ones whenever 1 is found, while reset 1_counter to 0 if 0 is encountered.

    int findMaxConsecutiveOnes(vector<int>& nums) {
           int max_count=0,save_count=0;
           for(int i=0;i<nums.size();i++){
               if(nums[i]==1){
                   save_count++;
                   max_count=save_count>max_count?save_count:max_count;
               }
               else{
                   save_count=0; 
               }        
           }
           
           return max_count;
    }
    

Log in to reply
 

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