Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.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.
class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { int nTimes = 0, maxOne = 0; int sz = nums.size(); for(int i = 0; i < sz; ++i){ if(nums[i] == 1){ ++nTimes; if(nTimes > maxOne) maxOne = nTimes; } else nTimes = 0; } return maxOne; }};新闻热点
疑难解答