# 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

# 示例:
# 输入: [-2,1,-3,4,-1,2,1,-5,4],
# 输出: 6
# 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
# 进阶:
# 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 (解法1)
 def maxSubArray(nums):
s, ts = - 2 ** 31, - 2 ** 31
res_ = []
for n in nums:
if n > ts + n:
ts = n
else:
ts = n + ts#如果当前和比n还小,当前最大和就取n,否则,取n+ts
if s < ts:
s = ts
print(res_)
return s res = maxSubArray([-2,1,-3])
print(res)

(解法2)分治法:

 

LeetCode--53 最大连续子序列(总结)的更多相关文章

  1. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  2. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  3. LeetCode——数组篇:659. 分割数组为连续子序列

    659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...

  4. Leetcode 659.分割数组为连续子序列

    分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...

  5. Java实现 LeetCode 659 分割数组为连续子序列 (哈希)

    659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...

  6. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  7. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  8. Leetcode#53.Maximum Subarray(最大子序和)

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

  9. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

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

  10. Maximum Subarray(最大连续子序列和)

    https://leetcode.com/problems/maximum-subarray/ 思路: 如果全为负值,那么取最大值 如果有非负值,那么我们依次计算到当前位置为止的最大值.假设有n个元素 ...

随机推荐

  1. 【Spring源码深度解析学习系列】注册解析的BeanDefinition(五)

    对于配置文件,解析和装饰完成之后,对于得到的beanDefinition已经可以满足后续的使用要求了,还剩下注册,也就是processBeanDefinition函数中的BeanDefinitionR ...

  2. 解决Ubuntu刚装好的时候su命令密码错误的问题

    Ubuntu刚安装后,在terminal中运行su命令会要求输入密码,然而无论输什么都会错,直接回车也是错,这因为root没有默认密码,需要手动设定.以安装ubuntu时输入的用户名登陆,该用户在ad ...

  3. PCB 铺铜 转载

    所谓覆铜,就是将PCB上闲置的空间作为基准面,然后用固体铜填充,这些铜区又称为灌铜.敷铜的意义在于,减小地线阻抗,提高抗干扰能力:降低压降,提高电源效率:还有,与地线相连,减小环路面积.如果PCB的地 ...

  4. Unity3D笔记 英保通五 鼠标事件与GUI系统双击检测

    一.如何使用GUI事件来检测鼠标是否按下的事件: 获取当前事件:var e:Event=Event.current: using UnityEngine; using System.Collectio ...

  5. github中删除一个repository

    (1) 首先进入相应的repository,然后点击setting 2,点击 delete the repository (3) 输入要删除的repository名字,即可删除

  6. wpgcms---碎片管理的使用

    这里很神奇的是碎片管理是编辑器,所以拿到的配置都是富文本,所以在前台作为字段来使用的时候,需要过滤掉字符串. 具体示例: {% set qq = wpg.fragment.get("qq&q ...

  7. Django---简单接受表单信息

    普通接受信息: 接受单选的值:例如:input select 等提交过来的信息 u = request.POST.get('username',None) 接受多选: h = request.POST ...

  8. ElasticSearch 安装 elasticsearch-analysis-ik分词器

    IK version ES version master 5.x -> master 5.6.1 5.6.1 5.5.3 5.5.3 5.4.3 5.4.3 5.3.3 5.3.3 5.2.2 ...

  9. Hdu1010Tempter of the Bone 深搜+剪枝

    题意:输入x,y,t.以及一个x行y列的地图,起点‘S’终点‘D’地板‘.’墙壁‘X’:判断能否从S正好走t步到D. 题解:dfs,奇偶性减枝,剩余步数剪枝. ps:帮室友Debug的题:打错了两个字 ...

  10. Python 字典 setdefault() 方法

    描述 Python 字典 setdefault() 方法和 get() 方法类似,返回指定键的值,如果键不在字典中,将会添加键并将值设置为一个指定值,默认为None. get() 和 setdefau ...