Leetcode #485 Max Consecutive Ones

来源:互联网 发布:gta5 handling原数据 编辑:程序博客网 时间:2024/06/03 17:09

Description

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

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

Example

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.

Code

class Solution(object):    def findMaxConsecutiveOnes(self, nums):        """        :type nums: List[int]        :rtype: int        """        cnt = 0        maxn = 0        for i in nums:            if i == 1:                cnt += 1                maxn = max(maxn, cnt)            else:                cnt = 0        return maxn
0 0
原创粉丝点击