485. Maximum number of consecutive 1s
Given a binary array, calculate the maximum number of consecutive 1s.
Example 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
note:
- The input array contains only 0 and 1.
- The length of the input array is a positive integer and does not exceed 10,000.
method one:
Simple questions, directly on the code.
Reference Code
public int findMaxConsecutiveOnes(int[] nums) {
int ans = 0, temp = 0;
for(int num : nums) {
if(num == 1) {
temp += 1;
continue;
}
ans = Math.max(ans, temp);
temp = 0;
}
ans = Math.max(ans, temp);
return ans;
}
Results of the