LeetCode53 最大子序列问题
题目描述:
给定一个整数数组 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 最大子序列问题的更多相关文章
- Leetcode53. 最大子序列和
问题 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 代码 贪心算法 核心思想就是检查之前 i-1 的元素和,如果小于零就舍弃--对应下面第六行 ...
- LeetCode--53 最大连续子序列(总结)
# 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. # 示例:# 输入: [-2,1,-3,4,-1,2,1,-5,4],# 输出: 6# 解释 ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- es6删除指定元素
原数组: let arr =[{id:1},{id:2},{id:3},{id:8}] 待删除数据 obj = {id:1} 删除原数组指定元素 arr.splice(arr.findIndex(it ...
- tensorflow学习笔记——DenseNet
完整代码及其数据,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/DeepLearningNote 这里结合网络的资料和De ...
- 使用tkinter打造一个小说下载器,想看什么小说,就下什么
前言 今天教大家用户Python GUI编程--tkinter 打造一个小说下载器,想看什么小说,就下载什么小说 先看下效果图 Tkinter 是使用 python 进行窗口视窗设计的模块.Tkint ...
- 【MySQL】Novicat 连接mysql 报错1251的问题处理,Novicat12 破解方法
1.远程连接时,报错 是因为我们的navicat版本太低 在网上查的是,出现这个原因是mysql8之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是ca ...
- Linux下网卡配置多个IP
ip addr add 192.168.12.4/24 dev eno16777728但是每次重启会失效 如果希望每次重启会重新绑定IP,可以将:ip addr add 192.168.12.X/24 ...
- SpringBoot + Layui + JustAuth +Mybatis-plus实现可第三方登录的简单后台管理系统
1. 简介 在之前博客:SpringBoot基于JustAuth实现第三方授权登录 和 SpringBoot + Layui +Mybatis-plus实现简单后台管理系统(内置安全过滤器)上改造 ...
- 面试 HTTP和HTML 浏览器
HTTP和HTML 浏览器 #说一下http和https #参考回答: https的SSL加密是在传输层实现的. (1)http和https的基本概念 http: 超文本传输协议,是互联网上应用最为广 ...
- create-react-app 基于ts项目,使用react-router-dom搭建项目
准备工作 来个react项目 create-react-app 基于TS的项目 ts项目安装后 删除node_modules,重新 yarn install, 不然jsx会报错 安装React-rou ...
- mini-web框架-装饰器-总结1(5.3.1)
@ 目录 1.说明 2.代码 关于作者 1.说明 原则:开放封闭 可以扩展,但是不可以修改.也就是说软件对扩展开放,对修改关闭. 运用技术:闭包 一共两种方法,一个使用闭包,一个使用类 使用多个装饰器 ...
- (第一篇)记一次python分布式web开发(利用docker)
作者:落阳 日期:2020-12-23 在一次项目开发中,决定使用docker+nginx+flask+mysql的技术栈来开发,用此系列文章记录开发的过程. 系列文章,当前为第一篇,记录一次pyth ...