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.

题目地址:  Maximum Subarray 

难度: Easy

思路: 最大子序列和问题

代码:

class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
tmp = 0
res = float('-inf')
for i in range(n):
if tmp < 0:
tmp = 0
tmp = tmp + nums[i]
res = max(res, tmp)
return res

时间复杂度: O(n)

空间复杂度: O(1)

最新文章

  1. 常用的 SQL 函数
  2. git 使用笔记
  3. Bull And Cows
  4. OpenSSl编译
  5. php文件处理
  6. poj1528---(数论)
  7. C# 数据库连接测试以及备份
  8. Dubbo Zookeeper
  9. OGRE HelloWorld
  10. 关于scrapy的piplines
  11. neo4j语法
  12. Python内置函数(21)——filter
  13. 大数据平台Lambda架构详解
  14. About the Importance of Aim in Life
  15. webservice学习教程(二)--理论
  16. Spring 学习教程(一):浅谈对Spring IOC以及DI的理解
  17. dialog里屏蔽ESC和回车
  18. Git中特别的命令
  19. OSG和ProLand 的海面仿真
  20. (最全)Xpath、Beautiful Soup、Pyquery三种解析库解析html 功能概括

热门文章

  1. VMWare虚拟机Windows下的下载与安装
  2. 第九组 通信3班 063 OSPFv2与OSPFv3综合实验
  3. 03.Jquery Mobile中的按钮
  4. 网站前端开发--css篇
  5. MySql提示:The server quit without updating PID file(…)失败之解决办法(来源网络仅供参考)
  6. yii2.0下,JqPaginator与Pjax实现无刷新翻页
  7. 基于.NetCore2.1。服务类库采用.Net Standard2.0,兼容.net 4.6.1消息推送服务
  8. 爬虫(GET)——handler处理器和自定义opener
  9. LINUX中IPTABLES防火墙使用
  10. webpack.config.js====entry入口文件的配置