题目

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

click to show more practice.

More practice:

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

 
代码:oj测试通过 Runtime: 80 ms
 class Solution:
# @param A, a list of integers
# @return an integer
def maxSubArray(self, A):
curr_sum = 0
max_sum = -100000
for i in range(len(A)):
if curr_sum < 0 :
curr_sum = 0
curr_sum = curr_sum + A[i]
max_sum = max(curr_sum, max_sum)
return max_sum

思路

想不出来这种精妙的算法。

隔了一天Google到了一篇解释日志:

http://blog.csdn.net/linhuanmars/article/details/21314059

这个把这道题的背后思想解释的很好,有些受益。

leetcode 【 Maximum Subarray 】python 实现的更多相关文章

  1. [leetcode]Maximum Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...

  2. LEETCODE —— Maximum Subarray [一维DP]

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

  3. LeetCode: Maximum Subarray 解题报告

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

  4. [LeetCode]Maximum Subarray题解

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

  5. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  6. 53. Maximum Subarray@python

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

  7. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. LeetCode——Maximum Subarray

    Description: Find the contiguous subarray within an array (containing at least one number) which has ...

  9. 53. [LeetCode] Maximum Subarray

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

  10. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

随机推荐

  1. Oracle Form个性化案例(一)

    业务场景: 现有Form A,需通过A中的菜单栏中调用另一Form B,需将某值作为参数传入Form B中:

  2. Tomcat中配置JAVA_HOME

    一般来说我们在使用Tomcat时配置JAVA_HOME都是采用的系统环境变量直接配置,下面我提供一种新的配置思路. 这里我们使用的是免安装版的apache-tomcat-7.0.35,首先我们安装好j ...

  3. 打包ios软件并发布到应用商店

    真心感慨程序员是一个神奇的动物. 昨天接到任务,将项目打包并发布到apple商店.于是乎... 利用Hbuilder打包 需要的3个文件: AppId,描述文件profile,以及私钥证书 必须条件: ...

  4. 日常-acm-排列

    用1-9组成三个数abc,def,ghi,每个数字恰好出现一次,要求abc:def:ghi=1:2:3.按照“abc def ghi”输出所有解,每行一个解. #include <iostrea ...

  5. wu2198:难得的波段抄底机会

    很好的波段抄底机会 个人浅见看,目前染料股跌出的机会明显,养殖股波段机会明显,芯片.半导体.集成电路.北导.软件.国产操作系统等科技股短线机会不错.另外,大盘指数2856/2806区域的波段操作机会不 ...

  6. fmt - 简易的文本格式优化工具 simple optimal text formatter

    总览 (SYNOPSIS) ../src/fmt [-DIGITS] [OPTION]... [FILE]... 描述 (DESCRIPTION) 重新 格式化 文件 FILE(s) 中的 每一个 段 ...

  7. 2018.5.21 . XMLSpy激活的方法

    127.0.0.1 altova.com #XMLspy 127.0.0.1 www.altova.com #XMLspy 127.0.0.1 link.altova.com #XMLspy 追加加到 ...

  8. 注册Windows service及其相关

    注册Windows service,.net写的 net stop "xxxxxx""%SYSTEMROOT%\Microsoft.NET\Framework\v2.0. ...

  9. javaweb基础(31)_国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...

  10. 黑马基础阶段测试题:通过字符输入流读取info.txt中的所有内容,每次读取一行,将每一行的第一个文字截取出来并打印在控制台上。

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...