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. day 02 python 基础

    1.day1作业讲解 题目答案见day1 2.格式化输出 %占位符,s:字符串,d:数字 %%只是单纯的显示%(显示的%是后面的) #格式化输出 # % s d # name = input('请输入 ...

  2. Vue的学习

    1.Vue是什么 参考Vue官网,一套用于构建用户界面的渐进式框架. 2.什么是渐进式框架 引用大神的话:“它给你提供足够的optional,但并不主张很多required,也不多做职责之外的事!这就 ...

  3. ubuntu安装smartGit

    1.首先安装jdk:(http://www.cnblogs.com/xiaochou/p/install_mint.html 评论区) 2.下载和安装smartGit: http://www.synt ...

  4. git hub 的使用步骤

    1:准备环境 ①电脑已安装git ②注册github账号 一:使用git控制台进行本地操作 ①打开 GitBash ②填写用户名和邮箱作为标识 分别输入以下命令: git config  --glob ...

  5. 面向对象DAO模式

    DAO模式编写数据访问层代码步骤? 1.  接口 增.删.改.查的方法 方法的返回类型为泛型集合 Int rows=select语句的条数 If(rows>0) 方法的返回类型为泛型集合 If( ...

  6. flask-bootstrap学习笔记

    flask-bootstrap 是flask一个方便扩展使用bootstrap前台的扩展. 使用方法: 安装: pip instal flask-bootstrap4 加载: from flask_b ...

  7. ubuntu上安装并使用mysql数据库

    一.安装Mysql 最简单的方式就是apt-get安装 安装核心程序 sudo apt-get install mysql-client-core-5.6 安装客户端程序 sudo apt-get i ...

  8. java中引用的概念

    强引用(StrongReference) 强引用就是指在程序代码之中普遍存在的,比如下面这段代码中的object和str都是强引用: 1 2 Object object = new Object(); ...

  9. 破解某PDF转换器产品

    本文章纯属出于作者自己对技术的探索,绝不用于商业用途(虽然网上已经能够下载到注册机了) 软件功能就不多说了,PDF转换成WORD格式,对于学生党来说也算是神器了吧,那么我们今天就用自己的办法来获得这款 ...

  10. ubuntu14/16 安装python3-opencv3_百度经验

    http://jingyan.baidu.com/article/e4511cf348dac52b845eafc8.html