close
Given a binary array, find the maximum number of consecutive 1s in this array.
Input: [1,1,0,1,1,1]
Output: 3
Explaniation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
第二題leetcode,雖然很簡單,還是紀錄一下吧哈哈。
解法:
寫一個迴圈,遇到1時就將目前記錄的長度+1
若此長度大於最大長度就max=now
遇到0時就將now歸零
再接再厲~
全站熱搜