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.

class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxsum=nums[0]
n=len(nums)
for i in range(1,n):
nums[i]=max(nums[i],nums[i]+nums[i-1])
maxsum=max(maxsum,nums[i])
return maxsum

  

[LeetCode&Python] Problem 53. Maximum Subarray的更多相关文章

  1. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  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 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  4. [LeetCode&Python] Problem 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [LeetCode&Python] Problem 559. Maximum Depth of N-ary Tree

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

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

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

  7. 53. Maximum Subarray【leetcode】

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

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

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

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

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

随机推荐

  1. 【转】IIS - 自动申请、部署Let's Encrypt的免费SSL证书

    IIS - 自动申请.部署Let's Encrypt的免费SSL证书(让网站实现HTTPS协议) 2017-12-19发布:hangge阅读:161   一.HTTPS 协议介绍 1,什么是 HTTP ...

  2. Linux系统启动过程(通俗易懂)

    前言: Linux是一种自由和开放源代码的类UNIX操作系统.该操作系统的内核由林纳斯·托瓦兹在1991年10月5日首次发布.在加上用户空间的应用程序之后,成为Linux操作系统.Linux是自由软件 ...

  3. java final static

    final: 修饰类:类不能被继承 修饰方法:方法不能被重写 修饰变量:不能修改变量的指向,且只能赋值一次 全局变量是有默认值的,所以如果用final修饰全局变量,能在定义的同时赋值,或在构造函数中赋 ...

  4. element 树形菜单加title

    <script src="//unpkg.com/vue/dist/vue.js"></script><script src="//unpk ...

  5. nodejs操作 mongoose(mongodb)和Sequelize(mysql)查询数据后添加新属性未生效

    最近在着手koa时候,发现mongoose(mongodb)查询数据库后添加新属性,前端拿不到新属性问题, 然后测试了一下Sequelize(mysql),发现也有同样的问题存在.此时着手干! 1.1 ...

  6. Unity中使用百度中文语音识别功能

    下面是API类 Asr.cs using System; using System.Collections; using System.Collections.Generic; using Unity ...

  7. c#实现数据库的备份

    在.NET开发后天管理系统的时候,数据库的备份功能是必须实现的一块,而在数据库备份方面一句sql语句就可以搞定了,那就是<Backup Database 数据库名To disk='路径\数据库备 ...

  8. spring的bean创建过程

    Spring的bean创建过程 步骤 执行过程 描述 1 ThreadLocal.set bean创建之前将beanName的一些属性放进ThreadLocal,避免多线程创建bean导致问题,并发创 ...

  9. MySQL【日期和时间处理函数】的使用方法

    名称 调用示例 示例结果 描述 NOW NOW() 2018-09-19 09:24:10 返回当前日期和时间 CURDATE CURDATE() 2018-09-19 返回当前日期 CURTIME ...

  10. [2003_p1]乒乓球

    一道因为输出不一样疯狂超时的题目(是我太菜,但是我jio得代码是ok的) 题目描述 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的 ...