485. Max Consecutive Ones

class Solution(object):
    def findMaxConsecutiveOnes(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        sumo=0
        output=0
        for i in nums:
            if i==1:
                sumo+=1
            elif i==0:
                output=sumo if sumo>output else output
                sumo=0
        return max([output,sumo])
     
秒了一题…甚至连bug都没de(好吧除了最后一个max)
但是只超过了30%多的人。

!!!等等,把最后一个max里面的[]去掉之后,超过了80%的人!!啥情况,研究一下。
好吧,好像就是这个生成list耽误了点时间。原来max里面可以接一串数字……我以为只能放iterable呢……是什么让我产生了这样的误解。。

class Solution(object): def findMaxConsecutiveOnes(self, nums): """ :type nums: List[int] :rtype: int """ highest = -1 count = 0 for num in nums: if num == 1: count += 1 else: if count > highest: highest = count count = 0 if count > highest: highest = count return highest

这是最快的人,思路是一样的,复杂度应该也是一样的,感觉我写的好一点,if else用的风骚一点。

评论

此博客中的热门博文

225 Implement Stack using Queues

232. Implement Queue using Stacks

20. Valid Parentheses