Given a binary array nums
, calculate the largest continuous 1
number in it.
Example 1:
Input: nums = [1,1,0,1,1,1] Output: 3 Explanation: The first two digits and the last three digits are consecutive 1s, so the maximum number of consecutive 1s is 3.
Example 2:
Input: nums = [1,0,1,1,0,1] Output: 2
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
dp = [0] * (len(nums)+1) # 初始化
for i in range(len(nums)): # 开始遍历
if nums[0] == 1: # 特殊情况,特殊处理
dp[0] = 1
if nums[i] == 1 and i > 0: # 一般情况处理
dp[i] = dp[i-1] + 1
elif nums[i] == 0:
dp[i] = 0
return max(dp)