题目来源

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. BZOJ3787 : Gty的文艺妹子序列

    将序列分成$\sqrt{n}$块,预处理出每两块之间的逆序对数,以及ap[i]表示前i块内数字出现次数的树状数组 预处理:$O(n\sqrt{n}\log n)$ 修改时,ap[i]可以在$O(\sq ...

  2. BZOJ3734 : [Ontak2013]Miny

    将所有炸弹按坐标排序 x<-y连边表示x爆炸了y也会爆炸 如果是DAG则直接拓扑排序+DP求出每个点出发能走到的最左端和最右端的点 有环则SCC缩点后再拓扑 用线段树优化建图的过程 边数$O(n ...

  3. POJ 1064 (二分)

    题目链接: http://poj.org/problem?id=1064 题目大意:一堆棍子可以截取,问要求最后给出K根等长棍子,求每根棍子的最大长度.保留2位小数.如果小于0.01,则输出0.00 ...

  4. 【BZOJ】2802: [Poi2012]Warehouse Store(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2802 自己yy了一下... 每一次如果够那么就买. 如果不够,考虑之前买过的,如果之前买过的比当前花 ...

  5. BZOJ3513: [MUTC2013]idiots

    Description 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. Input 第一行T(T<=100),表示数据组数.接下来若干行描述T组数据,每组数据第一行是n ...

  6. Jquery_JQuery之DataTables强大的表格解决方案

    1.DataTables的默认配置 $(document).ready(function() { $(‘#example’).dataTable(); } ); 示例:http://www.guoxk ...

  7. linux开启ssh服务

    本文概略:1)ubuntu发行版开启ssh.2)centos发行版开启ssh 1.ubuntu发行版安装/开启ssh服务 1.1 安装ssh服务端 sudo apt-get install opens ...

  8. VTK 6 和 VTK 5 的不同

    Overview Replacement of SetInput() with SetInputData() and SetInputConnection() Removal of GetProduc ...

  9. wordpress安装

    通过浏览器访问wordpress文件包 点击现在就开始,填写下面内容 我的填写 如出现下面情况,你得先创建一个数据库,再重试 数据库的创建 之后会出现 点击进行安装 安装成功 登录 主界面 写个文章, ...

  10. Bind的DNS解析设置forward - 阿权的书房

    bind有一项功能是forward,也就是只是做dns缓存. 适用以下情况: 1 做临时测试,不想向上级申请修改ns记录2 不想向上级申请修改ns记录但希望交给另外的服务器处理3 只有有限的公网IP, ...