1 class Solution: 2 def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 3 maxCount = count = 0 4 5 for i, num in enumerate(nums): 6 if num == 1: 7 count += 1 8 else: 9 maxCount = max(maxCount, count) 10 count = 0 11 12 maxCount = max(maxCount, count) 13 return maxCount
count = 0 ans = 0 for i in nums: if i == 1: count += 1 if ans < count: ans = count else: count = 0 return ans
#先化为str,再用split方法将其以"0"分隔,结果便是list中最长的item的长度 s = "" for i in nums: s += str(i) L2 = s.split("0") return max([len(i) for i in L2])
#快慢同向双指针(移动窗口法) slow = fast = 0 n = len(nums) max_count = 0 while slow < n: # 找出连续1中,第一个1的位置 if nums[slow] != 1: slow += 1 else: # fast指针从slow指针开始遍历,直至nums[fast]!=1 fast = slow while fast < n and nums[fast] == 1: fast += 1 # 局部最长fast-slow和全局最长max_count取最大值 max_count = max(max_count, fast - slow) # 更新slow指针的位置到fast指针处,继续循环,寻找下一个1 slow = fast return max_count
标签:count,slow,nums,max,fast,27,maxCount,485,283 From: https://www.cnblogs.com/wuyijia/p/17653625.html