题目描述:

给定一个整数数组 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. CSP-S2020复赛游记

    [本文经过删改] 前一个月 没做什么 NOIP 的题,感觉这些题对我这个做黄题封顶的人不是很友好. 前一天 考了场模拟赛,全场最低分 89,感觉信心满满. 退役那天 到了 XJ,发现没人可以面基,想着 ...

  2. 搭建yum仓库服务器

    环境:服务端centos6.9 客户端要求 能上网(可以ping通baidu.com) 1.yum的配置文件信息在/etc/yum.repos.d/下,我们配置的是自己的网络yum源,所以这些文件我们 ...

  3. Python+Selenium基本语法

    对Selenium自动化已有了解,最近开始做h5端的自动化,所以总结了下Python+Selenium自动化基本语法 一.启动浏览器 1.普通方式启动 #coding=utf-8 import tim ...

  4. gnuplot取消曲线标题

    plot 'File.dat' using 1:2 notitle或者 plot 'File.dat' using 1:2 title ""

  5. 来感受Linux命令行的“真香定律”

    Shell看起来只是一个黑黑的命令框,刚开始接触会觉得很丑,毕竟与Win/Mac的华丽界面比起来,命令行终端直接可以丑拒了.但是,实际上它的功能要强大得多,毕竟Linux一开始就是广泛应用于服务器,通 ...

  6. java.net.BindException:Problem binding to [hostname:8088]地址已在使用

    异常提示端口号被占用 查找被占用的端口 netstat -tln netstat -tln | grep 8083 netstat -tln ## 查看端口使用情况,而netstat -tln | g ...

  7. Django 3.x 原生支持websocket 配置

    websocket.py 1 # websocket.py 2 async def websocket_application(scope, receive, send): 3 while True: ...

  8. Python高级语法-对象实例对象属性-Property总结(4.6.2)

    @ 目录 1.说明 2.代码 关于作者 1.说明 property属性,返回的是值 不是callable的,也就是不能使用方法来调用 只能传入self,不能传入其他 用处,能返回局部数据,比如当分页的 ...

  9. JavaScript实现自定义右键菜单

    JavaScript实现自定义右键菜单,思路如下: 1. 屏蔽默认右键事件: 2. 隐藏自定义的菜单模块(如div.ul等): 3. 右键点击特定或非特定区域,显示菜单模块: 4. 再次点击,隐藏菜单 ...

  10. fiddler抓APP的https接口

    吐槽一下,fiddler这工具很蛋疼,用的时候很好用,出bug的时候死活抓不了: ①为了少踩坑,我们在官网下最新的fiddler,官网:Fiddler - Free Web Debugging Pro ...