当前位置:网站首页>Leetcode array question brushing notes

Leetcode array question brushing notes

2022-07-05 13:54:00 Cwxh0125

485. Maximum continuous 1 The number of

Given a binary array , Calculate the maximum continuity 1 The number of .       

Ideas : Let's go through the array first , Use a variable count To record 1 The number of When I meet 1 Change quantity ++, If encountered 0 Return to zero . You also need to use a variable maxcount To record the maximum

Method 1

int findMaxConsecutiveOnes(int* nums, int numsSize){
int count=0,maxCount=0;          // Define two variables 
for(int i=0;i<numsSize ;i++){    // Traversal array 
if(nums[i]==1)                    // if   The value is 1 be  count++
{count++;}                        
else {
    maxCount=fmax(count,maxCount);     // If zero is encountered   Then make the current variable value and  maxcount Compare the largest  
    count=0;                             // After zero   send count Zero value 
}  
                                               
} 
maxCount=fmax(count,maxCount);    
return maxCount;
}

原网站

版权声明
本文为[Cwxh0125]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140519232486.html