1、题目

53. Maximum Subarray——Easy

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

2、我的解法

(1)超时的暴力解法

 # -*- coding: utf-8 -*-
# @Time : 2020/2/6 22:15
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 53. Maximum Subarray(超时的暴力算法).py
from typing import List class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxSum = -9999999999
if len(nums)>1:
maxSum = -99999
for i in range(0, len(nums)):
for j in range(i, len(nums)):
thisSum = sum(nums[i:j+1])
if thisSum > maxSum:
maxSum = thisSum
return maxSum
elif len(nums)==1:
maxSum=nums[0]
return maxSum
else:
return 0 print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))

(2)局部最大值(copy大神)

 # -*- coding: utf-8 -*-
# @Time : 2020/2/7 16:12
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 53. Maximum Subarray.py from typing import List
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
tmp = 0
res = float('-inf')
for i in range(n):
if tmp < 0:
tmp = 0
tmp = tmp + nums[i]
res = max(res, tmp)
return res
print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))

LeetCode练题——53. Maximum Subarray的更多相关文章

  1. 刷题53. Maximum Subarray

    一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...

  2. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  3. [LeetCode&Python] Problem 53. Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  4. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  5. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  6. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  7. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

随机推荐

  1. 【STM32H7教程】第59章 STM32H7的DAC基础知识和HAL库API

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第59章       STM32H7的DAC基础知识和HAL库 ...

  2. QT5.1+中文乱码问题

    原文连接:https://blog.csdn.net/liyuanbhu/article/details/72596952 QT中规定 QString 的 const char* 构造函数是调用 fr ...

  3. Tensorflow机器学习入门——cifar10数据集的读取、展示与保存

    基本信息 官网:http://www.cs.toronto.edu/~kriz/cifar.html 共60000张图片:50000张用于训练.10000张用于测试 图片大小为:32X32 数据集图片 ...

  4. Scale9Sprite 的 setCapInsets中需要注意的地方

    在设置 setCapInsets()方法的y参数的时候,不直接取cocosStudio中的y,而是取Scale9Sprite.height - cocosStudio中的y.

  5. How to make mail more effectively?

    1.What does your reader need to know? 2.What does your reader know already? 3.Will your reader be ab ...

  6. 7.log4j

    Log4j:日志工厂的一部分(使用起来比较麻烦) 1.要想使用外部类,得先导包 pom.xml <dependency> <groupId>log4j</groupId& ...

  7. 转载:HRTF virtaul surround

    https://blog.csdn.net/Filwl_/article/details/50503558 https://blog.csdn.net/lwsas1/article/details/5 ...

  8. EAC3 mantissa quantization(VQ & GAQ)

    EAC3基于hebap来决定mantissa的quantizer. hebap如下: mantissa 使用VQ(vector quantization) 和GAQ(gain adaptive qua ...

  9. NW.js构建桌面应用

    最近要做个桌面的应用,用起来也方便.找了一圈发现NW.js挺容易上手,分享给大家. NW.js 官网https://nwjs.io/ 1.下载适合当前版本的js [这里下载的SDK版本,方便后续调试] ...

  10. 《aelf经济和治理白皮书》重磅发布:为DAPP提供治理高效、价值驱动的生态环境

    2020年2月17日,aelf正式发布<aelf经济和治理白皮书>,这是aelf继项目白皮书后,在aelf网络经济模型和治理模式方面的权威论述.<aelf经济和治理白皮书>描述 ...