Given a binary array, find the maximum number of consecutive 1s in this array.
Note:
1、The input array will only contain 0 and 1.
2、The length of input array is a positive integer and will not exceed 10,000
题意:数字1的最大连续长度
思路:遍历
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int max = 0;
int temp = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 1) {
temp++;
if (temp > max)
max = temp;
}
else
temp = 0;
}
return max;
}
};