从零开始的LC刷题(101): Max Consecutive Ones

mac2022-06-30  26

原题:

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

 

Note:

The input array will only contain 0 and 1.The length of input array is a positive integer and will not exceed 10,000

求出最长连续1的数字串的长度,结果: Success

Runtime: 36 ms, faster than 82.71% of C++ online submissions for Max Consecutive Ones.

Memory Usage: 11.7 MB, less than 87.88% of C++ online submissions for Max Consecutive Ones.

代码:

class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int cnt=0; int max=0; for(int i=0;i<nums.size();i++){ if(nums[i]){cnt++;continue;} else if(cnt>max){max=cnt;} cnt=0; } if(cnt>max){max=cnt;} return max; } };

 

最新回复(0)