首页 > 其他 > 详细

LeetCode 485 Max Consecutive Ones 解题报告

时间:2019-03-24 10:26:31      阅读:102      评论:0      收藏:0      [点我收藏+]

题目要求

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

题目分析及思路

给定一个01数组,要求找到这个数组中连续1的最大长度。可以考虑0的位置,结合数组切片,循环进行。要记得把最后数组的长度加上。

python代码 

class Solution:

    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:

        if len(set(nums)) == 1 and nums[0] == 1:

                return len(nums)

        ones = []

        while 0 in nums:

            zero = nums.index(0)

            ones.append(zero)

            nums = nums[zero+1:]

        ones.append(len(nums))

        return max(ones)

            

        

       

        

 

LeetCode 485 Max Consecutive Ones 解题报告

原文:https://www.cnblogs.com/yao1996/p/10587078.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!