Given a binary array, find the maximum number of consecutive 1s in this array.
Example:
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
.
Approach:
C++
#include <bits/stdc++.h>using namespace std;int findMaxConsecutiveOnes(vector<int> &nums){int res = 0;int cnt = 0;int n = nums.size();if (n == 0)return res;int i = 0;while (i < n){if (nums[i] == 1){cnt++;i++;}else{res = max(res, cnt);i++;cnt = 0;}}res = max(res, cnt);return res;}int main(){vector<int> nums = {1, 1, 0, 1, 1, 1};cout << findMaxConsecutiveOnes(nums);return 0;}
No comments:
Post a Comment