题目链接:https://leetcode.com/problems/maximum-subarray/

算法类型:动态规划

题目分析:最大序列和

代码实现:

 class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_len = len(nums)
if nums_len == 1:
return nums[0]
mid_len = nums_len / 2
left_max = self.maxSubArray(nums[ : mid_len])
right_max = self.maxSubArray(nums[mid_len : ]) mid_max_1 = nums[mid_len]
temp = mid_max_1
for i in range(0, mid_len)[::-1]:
temp += nums[i]
mid_max_1 = max(temp, mid_max_1) mid_max_2 = nums[mid_len]
temp = mid_max_2
for item in nums[ mid_len + 1 : ]:
temp += item
mid_max_2 = max(temp, mid_max_2) mid_max = mid_max_1 + mid_max_2 - nums[mid_len] return max(left_max, right_max, mid_max)

leetcode--Maximum Subarray的更多相关文章

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

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

  2. LeetCode: Maximum Subarray 解题报告

    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) whic ...

  4. [LeetCode] Maximum Subarray Sum

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

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

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

  6. [leetcode]Maximum Subarray @ Python

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

  7. LeetCode——Maximum Subarray

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

  8. 53. [LeetCode] Maximum Subarray

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

  9. Python3解leetcode Maximum Subarray

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

  10. LeetCode Maximum Subarray (最大子段和)

    题意: 给一个序列,求至少含一个元素的最大子段和? 思路: 跟求普通的最大子段和差不多,只不过需要注意一下顺序.由于至少需要一个元素,所以先将ans=nums[0].接下来可以用sum求和了,如果小于 ...

随机推荐

  1. 通过中看不中用的代码分析Ioc容器,依赖注入....

    /** * 通过生产拥有超能力的超人实例 来理解IOC容器 */ //超能力模组接口 interface SuperModuleInterface{ public function activate( ...

  2. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  3. dubbox 的各种管理和监管

    dubbo官方自带了dubbo-admin及dubbo-simple/dubbo-monitor-simple二个子项目用于服务治理及服务监控. 一.dubbo-admin的部署 这个比较简单,编译打 ...

  4. 服务端跨域处理 Cors

    1  添加 System.Web.Cors,System.Web.Http.Cors 2 global文件中 注册asp.net 管道事件 protected void Application_Beg ...

  5. [Python基础知识]正则

    import re str4 = r"^http://qy.chinahr.com/cvm/preview\?cvid=\w{24,25}&from=sou&gtid=\w{ ...

  6. Chrome浏览器调试,console简述

    作为一个前端开发者,不可避免的需要进行各种各样的调试. 在谷歌浏览器出来以前,火狐的firebug是特别有名的一款调试工具,不过自从谷歌浏览器诞生以来,其自带的开发者工具足以媲美firebug,某种程 ...

  7. DataSet与DataTable与DataRow的关系

    1. 添加数据: DataRow dr=MyDataSet.Tables["User"].NewRow(); dr["UserName"] = "张三 ...

  8. 【Mysql】 局域网远程连接问题

    设置了 user 表 的 host为‘%’ 为什么局域网还是连接不上: 新建查询-->分别执行 1.GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTI ...

  9. css:子元素div 上下左右居中方法总结

    最近在面试,不停地收到了知识冲击,尤其是对于一些基础的css.html.js问题居多,所以自我也在做反思,今天就css问题,如何让一个子元素div块元素上下左右居中 (以下总结方法,都已得到验证). ...

  10. cosbench 压测RGW生产环境

    配置信息: 使用  2个万兆网卡的RGW主机,并用OSPF做高可用和负载均衡.  Ceph OSD集群 有21Node (万兆网卡+ 12个4T SATA机械硬盘) 测试VM配置: 使用在使用VXLA ...