485. Max Consecutive Ones
难度: Easy
刷题内容
原题连接
- https://leetcode.com/problems/max-consecutive-ones
内容描述
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
解题方案
思路 1 **- 时间复杂度: O(N)*- 空间复杂度: O(N)***
- 使用temp保存每个0之间的差值
- 找出最大的差值即可
代码:
/**
* @param {number[]} nums
* @return {number}
*/
var findMaxConsecutiveOnes = function(nums) {
var max = 0,temp = 0;
for(var i =0;i<nums.length;i++){
nums[i]===0?temp=0:temp++;
max = Math.max(temp,max);
}
return max;
};