题目来源

https://leetcode.com/problems/maximum-subarray/

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.


题意分析


Input: a list

Output: a number

Conditions:求最大连续子序列的和


题目思路


这题有点像动态规划,但是发现不需要索引之类的,所以想直接知道以某个值为结尾的最大子串的和,用d[]来存储该和,然后遍历一次即可,注意用max值来保存所需,不断更新max值

做完之后发现其实没必要用一个数组保存所有的和,直接用另一个变量保存前一个位置的最大和即可,代码是用list保存的没有改进= =


AC代码(Python)


 __author__ = 'YE'

 class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
if length == 0:
return 0
if length == 1:
return nums[0] d = [0 for i in range(length)] d[0] = nums[0]
max = d[0] for i in range(1, length):
d[i] = nums[i] + d[i - 1] if (nums[i] + d[i - 1]) > nums[i] else nums[i]
if d[i] > max:
max = d[i] return max s = Solution()
nums = [-2,1,-3,4,-1,2,1,-5, 4]
print(s.maxSubArray(nums))

[LeetCode]题解(python):053-Maximum Subarray的更多相关文章

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

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

  2. 【LeetCode】053. Maximum Subarray

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

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

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

  4. LeetCode练题——53. Maximum Subarray

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

  5. Java for LeetCode 053 Maximum Subarray

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

  6. LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

  7. LeetCode之“动态规划”:Maximum Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  8. [LeetCode]题53:Maximum Subarray

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

  9. [leetcode.com]算法题目 - Maximum Subarray

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

  10. 053 Maximum Subarray 最大子序和

    给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...

随机推荐

  1. POJ 3661 (线性DP)

    题目链接: http://poj.org/problem?id=3661 题目大意:牛跑步.有N分钟,M疲劳值.每分钟跑的距离不同.每分钟可以选择跑步或是休息.一旦休息了必须休息到疲劳值为0.0疲劳值 ...

  2. 【BZOJ】1927: [Sdoi2010]星际竞速(费用流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1927 题意:n个点的无向图.m条加权边.只能从编号小的到编号大的.可以瞬移,瞬移有时间.每个点只能访 ...

  3. 【wikioi】1026 逃跑的拉尔夫

    题目链接 算法:BFS 14.01.02 PS: 本人再次脑残,BFS又是写得那么脓肿,突然发现我原来什么搜索都是不会的呀.. //2014-02-05已更新 ******************** ...

  4. Linux(Redhat)下redis安装

    原文:http://www.javaweb1024.com/data/NoSQL/2015/06/29/785.html redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系 ...

  5. jBPM4.3+ssh+会签 整合配置及完整实例

    大佬们的项目里有用到会签,所以趁双休日研究了下. 其实也是简单的会签情况,不过开始的时候研究了4.4,(因为先前研究的都是4.4),发现4.4跟4.3的处理方法完全不一样,搞的我比较郁闷……弄了一天, ...

  6. 使用SBT构建Scala应用(转自git)

    # 使用SBT构建Scala应用 ## SBT简介 SBT是Simple Build Tool的简称,如果读者使用过Maven,那么可以简单将SBT看做是Scala世界的Maven,虽然二者各有优劣, ...

  7. Metasploit 笔记

    目录一.名词解释···································································· 3二.msf基础··············· ...

  8. Javascript 笔记与总结(1-4)this

    js 中函数的 4 种调用方式: ① 作为普通函数来调用,this 的值指向 window,准确地说,this 为 null,被解释成为 window.在 ECMAScript5 标准中,如果 thi ...

  9. javascript Array类

    Array类 toString()方法和valueOf()方法,返回特殊的字符串.该字符串是通过对每项调用toString()方法,然后用逗号把它们连接在一起构成的.例如,对具有项"red& ...

  10. 运维工程师必会的109个Linux命令

    运维工程师必会的109个Linux命令 版本1.0 崔存新 更新于2009-12-26 目录 1 文件管理 6 1.1 basename 6 1.2 cat 6 1.3 cd 7 1.4 chgrp ...