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 #include <iostream> #include <vector> using namespace std; class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int r=0,count=0; vector<int> c; if(nums.size() == 1) { if(nums[0] == 0) return 0; if(nums[0] == 1) return 1; } else { for(int i=0;i<nums.size();i++) { if(nums[i] == 0 && i+1!=nums.size()) count+=0; else if(nums[i] == 0 && i+1 == nums.size()) { c.push_back(0); } else if(nums[i] == 1 && nums[i+1] == 0) { count++; c.push_back(count); count = 0; } else if(nums[i] == 1 && i+1 == nums.size()) { count++; c.push_back(count); } else if(nums[i] == 1 && nums[i+1] == 1) { count++; } } } r = c[0]; for(int j=1;j<c.size();j++) { if(r < c[j]) r = c[j]; } return r; } }; int main() { Solution r; vector<int> nums; nums.push_back(0); nums.push_back(0); cout << r.findMaxConsecutiveOnes(nums) << endl; return 0; }