#485 Max Consecutive Ones

2017-04-28  本文已影响0人  乐正君Krizma

题目:

Given a binary array, find the maximum number of consecutive 1s in this array.



代码:

var findMaxConsecutiveOnes = function(nums) {    

let ans = 0, tmp = 0, len = nums.length;   

nums[len] = 0;   

 for(let i=0;i<len+1;++i){

if (tmp != tmp+nums[i]) {

tmp += nums[i];

} else {

ans = (ans>tmp)?ans:tmp;

tmp = 0;

}

}

return ans;

};

上一篇下一篇

猜你喜欢

热点阅读