1. 程式人生 > >485. Max Consecutive Ones (最大連續數) by Python

485. Max Consecutive Ones (最大連續數) by Python

大連 put int statistic inpu one emp bin 簡單

485. 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連續的數目

思路:

很簡單的列表遍歷,要註意的就是最後一次遍歷的值也要進行比較

結果:

class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_num = 0
statistics = 0
# 遍歷列表
for temp in nums:
if temp == 1:
statistics += 1
else:
if statistics > max_num:
max_num = statistics
statistics = 0
# 保證最後一遍也能進行比較
if statistics > max_num:
max_num = statistics
return max_num

485. Max Consecutive Ones (最大連續數) by Python