题目描述:

给定一个整数数组 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. 配置 Spring Batch 批处理失败重试机制

    1. 引言 默认情况下,Spring批处理作业在执行过程中出现任何错误都会失败.然而有些时候,为了提高应用程序的弹性,我们就需要处理这类间歇性的故障. 在这篇短文中,我们就来一起探讨 如何在Sprin ...

  2. Editor.md解决跨域上传的问题

    Editor.md解决跨域上传的问题 编辑 editormd\plugins\image-dialog\image-dialog.js 替换以下代码片段 if (settings.crossDomai ...

  3. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes

    hive 安装完成后创建表的时候出现错误 NestedThrowablesStackTrace:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorExce ...

  4. 想用selenium ,先了解html 基础知识(5)

    二.HTML语法---了解!1.HTML超文本标记语言,是网页设计使用的语言.2.从<html>开始,到</html>结束,里面包括head和body两个部分,我们测试人员关心 ...

  5. 移动端 canvas统计图

    一.折线图 1. 获取画布画笔 2. 封装画线的方法 3. 画坐标轴 4. 循环数据画横轴 > 4.1 画刻度 > 4.2 刻度文字 > 4.3 画折线 > 4.4 画点 5. ...

  6. Hbase各组件职责

    Hbase各组件职责 Client职责 1.HBase有两张特殊表: .META.:记录了用户所有表拆分出来的的Region映射信息,.META.可以有多个Regoin -ROOT-:记录了.META ...

  7. Python高级语法-多继承MRO相关-args和kwargs(4.5.2)

    @ 目录 1.说明 2.代码 关于作者 1.说明 args数据类型为元组 kwargs数据类型为字典 一般传入方法中使用遍历去得到值 这个传入参数的顺序没有特殊的要求 当你自定义的参数传完以后,写了名 ...

  8. vscode php转到定义

    点击再settings.json 中编辑 添加这一行,内容为php的安装路径

  9. Centos7路由设置

    再添加路由时,很多时候都是采用命令行用route添加的.但是在机器重启后.就失效了.这里也是参考了几位博主的经验 作出以下记载 一:路由表常用设置 1.route命令路由表常用设置: //添加到主机的 ...

  10. H3C S5120V2-SI 交换机配置

    连接终端线 可以看到开机信息 ......................................................................Done. System is ...