---
# System prepended metadata

title: \#485 Max Consecutive Ones
tags: [array, leetcode, leetcode-easy]

---

# \#485 Max Consecutive Ones
## *給定一數字陣列nums, 內含元素0或1, 回傳該陣列中連續出現1的最大次數*
## Log
 - build 20210913 by syhuang

## 改良
 - 陣列迴圈計算長度
 - leetcode submit runtime 77%, memory 83%
```javascript=
var findMaxConsecutiveOnes = function(nums) {
    let count = 0;
    let result = 0;
    nums.forEach(function(value,index){
        if(value) count++;
        else{
            result = Math.max(count,result);
            count = 0;
        }
    });
    return Math.max(count,result);
};
```
## 初見
 - 將陣列轉字串後split('0')排序回傳最長長度
 - leetcode submit runtime 77%, memory 9%
```javascript=
var findMaxConsecutiveOnes = function(nums) {
    return nums.join('').toString().split('0').sort(
        function (a, b) {
            return b.length - a.length;
    })[0].length;
};
```
## 備註
- [參考解](https://leetcode.com/problems/max-consecutive-ones/discuss/96693/Java-4-lines-concise-solution-with-explanation)
## 參考
###### tags: `leetcode`, `leetcode-easy`, `array`