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. OpenCV中的霍夫线变换和霍夫圆变换

    一.霍夫线变换 霍夫线变换是OpenCv中一种寻找直线的方法,输入图像为边缘二值图. 原理: 一条直线在图像二维空间可由两个变量表示, 例如: 1.在 笛卡尔坐标系: 可由参数: (m,b) 斜率和截 ...

  2. MVC5+EF6 入门完整教程4 :EF基本的CRUD

    上篇文章主要讲了如何配置EF, 我们回顾下主要过程: 创建Data Model ---> 创建Database Context --->创建databaseInitializer---&g ...

  3. 【转】Git常用命令指南

    1.git init 初始化一个Git仓库,git init –bare example.git创建一个裸仓,即没有工作区的git仓库.2.添加文件到Git仓库,分两步:git add <fil ...

  4. 智能指针和异常、 weak_ptr、unique_ptr

    12.1.4智能指针和异常 1.在块中创建的动态内存,如果是由内置指针来指向这块内存,那么若是在块结束时未delete这个指针,则该内存不会被释放,若在delete之前发生异常,由于还没执行delet ...

  5. AC3 overview

    1.AC3 encode overview AC3 encoder的框图如下: AC3在频域采用粗量化(coarsely quantizing)来获取较高的压缩率. 1).输入PCM 经过MDCT变换 ...

  6. 2018-2019-20175334实验三《敏捷开发与XP实践》实验报告

    2018-2019-20175334实验三<敏捷开发与XP实践>实验报告 一.实验内容及步骤 实验三 敏捷开发与XP实践-1 实验三 敏捷开发与XP实践 http://www.cnblog ...

  7. nmonchart 分析.nmon监控数据成html展示

    下载地址:http://nmon.sourceforge.net/pmwiki.php?n=Site.Nmonchart chart安装包:http://sourceforge.net/project ...

  8. docker-api的使用(java)

    通过docker-api来执行docker相关的操作.   配置 可以在docker启动文件docker.service中加入如下 vi /lib/systemd/system/docker.serv ...

  9. C++的四种转换(const_cast、static_cast、dynamic_cast、reinterpreter_cast)

    static_cast 相当于C语言中的强制转换:(类型)表达式或类型(表达式),用于各种隐式转换 非const转const.void*转指针.int和char相互转换 用于基类和子类之间的指针和引用 ...

  10. JSON--WEB SERVICE

    Query ajax webservice:get 和 post 一.GET 方式 客户端 复制代码代码如下: var data = { classCode: "0001"}; / ...