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 ...
随机推荐
- Acwing 405. 将他们分好队
大型补档计划 题目链接 看到分成两组,想到二分图判定 + 染色. 二分图的特点是两个有矛盾的点连一条边,考虑在这道题中,如果 \(a, b\) 中有一个人不认识对方(或者两个人互不认识),就不可能分在 ...
- 题解-[HNOI2016]序列
题解-[HNOI2016]序列 [HNOI2016]序列 给定 \(n\) 和 \(m\) 以及序列 \(a\{n\}\).有 \(m\) 次询问,每次给定区间 \([l,r]\in[1,n]\),求 ...
- Python迭代器&生成器&装饰器
1. 迭代器 1.1 可迭代对象(Iterator) 迭代器协议:某对象必须提供一个__next__()方法,执行方法要么返回迭代中的下一项,要么引起一个Stopiteration异常,以终止迭代(只 ...
- hive中的虚拟列
hive为用户提供了三个虚拟列:用户可以通过这三个虚拟列确定记录是来自哪个文件以及这条记录的具体位置信息 INPUT__FILE__NAME 返回记录所在的具体hdfs文件全路径 hive> s ...
- 07-flask-使用sqlalchemy
代码 from flask import Flask, render_template from sqlalchemy import create_engine from sqlalchemy.ext ...
- kali linux没有ip解决办法
故障情况 今天打开kali202001复测环境,发现自启动ssh竟然连不上. 上到kali主机使用命令:ifconfig 查看发现没有ethh0网卡显示,看来是kal获取不到ip地址导致的 继续查看发 ...
- SpringBoot从入门到精通教程(一)
写在前面的话: 在很早之前,记笔记时候,我就一直在思考一个问题,我记笔记是为了什么,我一直想不明白 ,后面发现技术跟新迭代的速度实在太快了,笔记刚纪完,技术又跟新了,于是我想了想干脆边写博客,边记笔记 ...
- 持久层之 MyBatis: 第二篇 :动态SQL And多表查询
MyBatis入门到精通 完整CRUD UserDaoImpl 编写UserDao对应的UserDaoMapper.xml 添加UserDao的测试用例 编写UserDao的测试用例 解决数据库字段名 ...
- 讲两个int 数组找出重复的数字 用最少的循环
int a[] = {1,3}; int b[] = {1,3,5}; int size = a.length>b.length ?a.length:b.length; int valueA = ...
- Qt学习笔记-中文乱码-QtWebkit显示网页乱码的问题QFont::setPixelSize: Pixel size <= 0 (0)
在使用webkit的时候显示中文乱码,同时出现警告 QFont::setPixelSize: Pixel size <= 0 (0) 后来发现是插件的问题. 将安装目录的plugin/codec ...