题目描述:

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
    示例:
    输入: [-2,1,-3,4,-1,2,1,-5,4],
    输出: 6
    解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

找到一组数中的另一个子序列,使子序列的和最大,拿到一个数,这个数如果即加上他之前的的所有数的和,与他原先相比增大了,那么就选取大的数
     * 暂时作为这个数组中的子序列之和的最大值,反之也一样,arr[i]就是记录  从0到i的元素的和 与nums[i]两者的最大值。

package cn.leetcode.test;

public class MaxSubArray {
public static void main(String[] args) {
int [] nums = {-2,1,-3,4,-1,2,1,-5,4};
int sum = maxSubArray(nums);
System.out.println(sum);
} public static int maxSubArray(int [] nums) {
if(nums.length==1) {//题目上至少包含一个元素
return nums[0];
}
int len = nums.length;
int arr[] = new int[len];
int result = nums[0];
arr[0] = nums[0];
for (int i = 1; i < arr.length; i++) {
arr[i] = Math.max(nums[i], nums[i]+arr[i-1]);//这一步是关键
result = Math.max(arr[i], result);
} return result;
} }

LeetCode53 最大子序列问题的更多相关文章

  1. Leetcode53. 最大子序列和

    问题 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 代码 贪心算法 核心思想就是检查之前 i-1 的元素和,如果小于零就舍弃--对应下面第六行 ...

  2. LeetCode--53 最大连续子序列(总结)

    # 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. # 示例:# 输入: [-2,1,-3,4,-1,2,1,-5,4],# 输出: 6# 解释 ...

  3. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  4. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  5. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  6. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  7. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  8. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  9. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. memcached session会话共享

    1 安装依赖包yum install libevent livevent-devel nc -y 2 yum 安装memcachedyum install -y memcached 3 启动memec ...

  2. DjangoRestFramework使用

    目录: 1.1 DjangoRestFramework基本使用 1.2 drf认证&权限 模块 1.3 djangorestframework 序列化 1.4 djangorestframew ...

  3. Linux系统-scp简介&坑

    文件请见这里: https://blog.csdn.net/xingxingzhilong/article/details/82909015

  4. oracle 常用语句2

    -- number(38) -- char(2000) -- varchar(4000) create table student( sno number(3) primary key, sname ...

  5. 面试 09-02.js运行机制:异步和单线程

    09-02.js运行机制:异步和单线程 #前言 面试时,关于同步和异步,可能会问以下问题: 同步和异步的区别是什么?分别举一个同步和异步的例子 一个关于 setTimeout 的笔试题 前端使用异步的 ...

  6. SPI机制剖析——基于DriverManager+ServiceLoader的源码分析

    我的上一篇博客类加载器与双亲委派中提到,SPI机制是一种上级类加载器调用下级类加载器的情形,因此会打破类加载的双亲委派模型.为了深入理解其中的细节,本博客详细剖析一下SPI机制,并以JDBC为例,基于 ...

  7. Python之word文档模板套用 - 真正的模板格式套用

    Python之word文档模板套用: 1 ''' 2 #word模板套用2:套用模板 3 ''' 4 5 #导入所需库 6 from docx import Document 7 ''' 8 #另存w ...

  8. burpsuite暴力破解之四种方式

    给出字典排列.详情: 1. 2. 第一项:snipper(中译:狙击手) 1.为两个参数添加payload并且选中snipper,同时指定一个字典. 2.开始attack,并且给出响应结果. 可见有两 ...

  9. Java获取某年某月的第一天和最后一天

    /** * 获取某年某月的第一天 * @Title:getFisrtDayOfMonth * @Description: * @param:@param year * @param:@param mo ...

  10. 【vue】常用指令

    vue指令带有前缀 v-. 一.v-bind 单向数据绑定 在html中显示数据,除了使用插值表达式{{}}之外,也可以使用vue中的v-bind指令. ... ... <body> &l ...