zoukankan      html  css  js  c++  java
  • leetcode python 217. 存在重复元素 53. 最大子序和

    217. 存在重复元素

    给定一个整数数组,判断是否存在重复元素。
    如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false

    class Solution(object):
        def containsDuplicate(self, nums):
            nums.sort()
            for i in range(len(nums)-1):
                if nums[i]== nums[i+1]:
                    return True
            return False
    

    执行用时:36 ms, 在所有 Python3 提交中击败了79.02%的用户

    内存消耗:17.5 MB, 在所有 Python3 提交中击败了91.44%的用户

    53. 最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

    输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
    输出:6
    解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
    
    class Solution:
        def maxSubArray(self, nums: List[int]) -> int:
            single = 0
            maxs = nums[0]
            for i in range(len(nums)):
                single += nums[i]
                maxs = max(single,maxs)
                if single < 0:
                    single = 0
            return maxs
    

    执行用时:36 ms, 在所有 Python3 提交中击败了80.39%的用户

    内存消耗:15.2 MB, 在所有 Python3 提交中击败了88.04%的用户

  • 相关阅读:
    golang 中 sync包的 WaitGroup
    Go_20: Golang 中 time 包的使用
    mysql 同步数据到 ElasticSearch 的方案
    mysql 对应 binlog 查看
    python3.6爬虫总结-01
    Golang 之协程详解
    golang私服搭建
    Ubuntu vim设置
    密码校验规则
    golang密码校验
  • 原文地址:https://www.cnblogs.com/hereisdavid/p/15260056.html
Copyright © 2011-2022 走看看