# 给定一个整数数组 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. JS方法 - 字符串处理函数封装汇总 (更新中...)

    一.计算一段字符串的字节长度 字符串的charCodeAt()方法, 可返回字符串固定位置的字符的Unicode编码,这个返回值是0-65535之间的整数,如果值<=255时为英文,反之为中文. ...

  2. linux下文件描述符的介绍

    当某个程序打开文件时,操作系统返回相应的文件描述符,程序为了处理该文件必须引用此描述符.所谓的文件描述符是一个低级的正整数.最前面的三个文件描述符(0,1,2)分别与标准输入(stdin),标准输出( ...

  3. vue Element动态设置el-menu导航当前选中项

    1,npm install vuex --save 2,在src下新建vuex文件夹,新建store.js文件: store.js import Vue from 'vue' import Vuex ...

  4. android开发,权限获取

    转:http://blog.csdn.net/yawinstake/article/details/6748897 访问登记属性 android.permission.ACCESS_CHECKIN_P ...

  5. [工具] 护眼宝 – 傻瓜版屏幕蓝光过滤应用[Win/Android]

    护眼宝 是一款 Windows.Android 下的屏幕蓝光过滤工具,傻瓜式操作,支持智能模式.疲劳提醒,可以有效的保护视力及减小夜间使用电脑.手机对睡眠对影响. 来自发现频道.   类似应用有很多了 ...

  6. [通信] C#多线程Socket-文件传输

    FileSendClient : Form1.cs using System; using System.IO; using System.Net; using System.Net.Sockets; ...

  7. vue--双向数据绑定

    <template> <div id="app"> <p>{{msg}}</p> <input v-model="m ...

  8. Shell for

    for循环一般格式为:for 变量 in 列表do command1 command2 ... commandNdone列表是一组值(数字.字符串等)组成的序列,每个值通过空格分隔.每循环一次,就将列 ...

  9. Heavy Transportation---poj1797

    求(Dijkstra算法,求每条路径上的最小值 的最大值)和青蛙的那题类似:   #include<iostream> #include<stdio.h> #include&l ...

  10. matplotlib基本使用方法

    [微语]人生有可为之事,也有不可为之事.可为之事,当尽力为之,此谓尽性,不可为之事,当尽心为之,此谓知命. 三人行必有我师 官方参考API:https://matplotlib.org/tutoria ...