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

思路:

  1. 如果全为负值,那么取最大值
  2. 如果有非负值,那么我们依次计算到当前位置为止的最大值。假设有n个元素,那么最大连续子序列只可能以0~n-1中某个位置结尾。当我们遍历到第i个元素时,判断以位置i-1为结尾的最大元素子序列和是否小于0,如果小于0,那么以位置i为结尾的最大连续子序列和为位置i对应的元素;否则,以位置i为结尾的最大连续子序列和为(以位置i-1为结尾的最大元素子序列和 + 位置i的元素)

代码

class Solution(object):def maxSubArray(self, nums):""" :type nums: List[int]:rtype: int"""max_sum = nums[0]max_sum_here = 0 for item in nums:if max_sum_here < 0:max_sum_here = itemelse:max_sum_here += itemif max_sum_here > max_sum:max_sum = max_sum_herereturn max_sumif __name__ == '__main__':s = Solution()print(s.maxSubArray([-2,1,-3,4,-1,2,1,-5,4])) 

转载于:https://www.cnblogs.com/renzongxian/p/5595317.html

Maximum Subarray(最大连续子序列和)相关推荐

  1. leetcode53 Maximum Subarray 最大连续子数组

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

  2. Maximum Sum UVA - 108(连续子序列最大和—变形之子矩阵最大和)

    题目大意:给出 n*n 的矩阵,找每隔数字之和最大的子矩阵,输出最大和.  解题思路:枚举矩阵左上和右下的坐标,分别合并子矩阵的每列,使得二维转化为一维,然后利用连续子序列最大和去做就行. Time ...

  3. 1007. Maximum Subsequence Sum (25)-PAT甲级真题(最大连续子序列和、动态规划dp)

    Given a sequence of K integers { N1, N2, -, NK }. A continuous subsequence is defined to be { Ni, Ni ...

  4. Leetcode-53 Maximum Subarray

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

  5. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  6. Lintcode42 Maximum Subarray II solution 题解

    [题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...

  7. 53.Maximum Subarray

    /** 53.Maximum Subarray * 2016-5-7 by Mingyang * 如果我们从头遍历这个数组.对于数组中的其中一个元素,它只有两个选择: 1.* 要么加入之前的数组加和之 ...

  8. LeetCode(53):Maximum Subarray

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

  9. 算法--最大连续子序列和(动态规划,分而治之)

    今天在LeetCode上遇到一个求最大连续子序列和的问题,如下: Given an integer array nums, find the contiguous subarray (containi ...

最新文章

  1. Python中使用you-get库批量在线下载bilibili视频的教程
  2. 拒绝亡羊补牢!SQL Server服务器安全防护!
  3. 设计模式---建造者模式(DesignPattern_Builder)
  4. 不要被泛型和向后兼容性所愚弄。 使用泛型类型
  5. 信息学奥赛一本通C++语言——1090:含k个3的数
  6. 类的静态成员函数和静态成员变量的使用
  7. 最新生成树之克鲁斯卡尔算法
  8. 第22.7节 性能篇-使用八叉树结构来管理场景
  9. C64x+ 与 C64x Cache 区别
  10. LeetCode 520. Detect Capital
  11. echarts 横向柱状图
  12. MATLAB生成随机数
  13. Porteus Kiosk
  14. Linux CentOS7.0 使用root登录桌面
  15. Android——一个简单的音乐APP(二)
  16. nodejs服务使用pm2启动多个进程和进程守护,以及使用pm2-logrotate-ext分割日志
  17. 橡胶支座抗压弹性模量计算公式_(完整版)橡胶支座计算
  18. Echart API
  19. 有没有什么靠谱的副业?
  20. 迅锐CMS模板常用调用总结

热门文章

  1. 关于推送的一些记录要点
  2. 富文本编辑器 CKeditor 配置使用
  3. 你可能不知道console强大
  4. 网管必知远程终端3389端口合理修改秘藉
  5. java 跨平台 ios_OGEngine发布跨平台版本,支持iOS及Android
  6. 1005打印任务取消不了 hp_惠普HP M1136 MFP多功能打印机 一年半使用感受(学生打印机)...
  7. 子类怎么继承父类方法中的变量_关于继承的那些事!
  8. STM32F103_SPI读写外部FLASH
  9. oracle 行数大于一时,oracle – PL / SQL ORA-01422:精确的提取返回超过请求的行数
  10. 【特征向量】——从线性代数角度看分解与合成