zoukankan      html  css  js  c++  java
  • leetcode——485.最大连续1的个数

    class Solution:
        def findMaxConsecutiveOnes(self, nums) -> int:
            res=[]
            m=0
            i=0
            while i<len(nums):
                if nums[i]==1:
                    i+=1
                    res.append(1)
                else:
                    m=max(m,len(res))
                    res=[]
                    i+=1
            m=max(m,len(res))
            return m
    执行用时 :456 ms, 在所有 Python3 提交中击败了76.89%的用户
    内存消耗 :14.1 MB, 在所有 Python3 提交中击败了5.14%的用户
    执行用时为 72 ms 的范例
    class Solution:
        def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
            res = 0
            temp = 0
            for i in nums:
                if i:
                    temp += 1
                else:
                    if temp > res:
                        res = temp
                    temp = 0
            return max(res,temp)
    执行用时 :508 ms, 在所有 Python3 提交中击败了64.28%的用户
    内存消耗 :13.8 MB, 在所有 Python3 提交中击败了10.54%的用户
     
    这个执行用时并不是严格可参考数据。
     

    我忽略了数组里面只包含0和1的这个条件。

    思路相同。

                                           ——2019.10.9

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11644064.html
Copyright © 2011-2022 走看看