当前位置:网站首页>#yyds干货盘点# 解决剑指offer: 连续子数组的最大和(二)
#yyds干货盘点# 解决剑指offer: 连续子数组的最大和(二)
2022-06-28 21:40:00 【51CTO】
1.简述:
描述
输入一个长度为n的整型数组array,数组中的一个或连续多个整数组成一个子数组,找到一个具有最大和的连续子数组。1.子数组是连续的,比如[1,3,5,7,9]的子数组有[1,3],[3,5,7]等等,但是[1,3,7]不是子数组2.如果存在多个最大和的连续子数组,那么返回其中长度最长的,该题数据保证这个最长的只存在一个3.该题定义的子数组的最小长度为1,不存在为空的子数组,即不存在[]是某个数组的子数组4.返回的数组不计入空间复杂度计算
数据范围:
要求:时间复杂度,空间复杂度
进阶:时间复杂度
,空间复杂度
示例1
输入:
返回值:
说明:
示例2
输入:
返回值:
示例3
输入:
返回值:
说明:
示例4
输入:
返回值:
说明:
2.代码实现:
import java.util.*;
public class Solution {
public int[] FindGreatestSumOfSubArray (int[] array) {
int x = array[0];
int y = 0;
int maxsum = x;
//滑动区间
int left = 0, right = 0;
//记录最长的区间
int resl = 0, resr = 0;
for(int i = 1; i < array.length; i++){
right++;
//状态转移:连续子数组和最大值
y = Math.max(x + array[i], array[i]);
//区间新起点
if(x + array[i] < array[i])
left = right;
//更新最大值
if(y > maxsum || y == maxsum && (right - left + 1) > (resr - resl + 1)){
maxsum = y;
resl = left;
resr = right;
}
//更新x的状态
x = y;
}
//取数组
int[] res = new int[resr - resl + 1];
for(int i = resl; i <= resr; i++)
res[i - resl] = array[i];
return res;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
边栏推荐
- Pat 1054 the dominiant color (20 points)
- PE file-
- In one sentence, I will tell you the meaning of select 1, 2 and 3 in SQL injection, and explain the meaning of each part of SQL injection in detail
- Workplace tips | understanding the advantages of the position "knowing people"
- Openfire 3.8.2 cluster configuration
- Is it safe to open a stock trading account? Is it reliable?
- What is an interface? What is interface testing?
- LeetCode986. Intersection of interval lists
- LeetCode213. House raiding II
- Golang JSON serializing and deserializing strings deserializing to map[string]interface{}
猜你喜欢

力扣树的进一步应用

17 `bs object Node name h3 Parent ` parents get parent node ancestor node

Ehcache configuration data, convenient for self checking

Lumiprobe proteorange protein gel dye instructions

AI deep dive of Huawei cloud

Query rewriting for opengauss kernel analysis

Live broadcast preview | can SQL also play industrial machine learning? Mlops meetup V3 takes you to the bottom!

【激活函数】

Alist+raidrive gives the computer a complete 8billion GB hard disk drive

rosdep update 使用小鱼fishros解决ros1/ros2问题 2022
随机推荐
职场小技巧 | 了解岗位优势三板斧之“识人”
[linq]c list type grouping sum
LxC shared USB device
Which software is safer to open an account on and what is the account opening process?
Postman introduction and installation steps
【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
Lumiprobe proteorange protein gel dye instructions
Recommend two high-quality Wallpaper software
LeetCode877. 石子游戏
视觉弱监督学习研究进展
QT how the coordinates of one control are relatively fixed and displayed on another control (coordinate system)
LeetCode116. Populate the next right node pointer for each node
Proficient in data analysis, double the income? What is the strongest competitiveness
【激活函数】
How to make up the PMP Exam? How much is the make-up exam?
The rogue downloader named by 315 is back
Openfire user and group relationship migration
Query rewriting for opengauss kernel analysis
LeetCode226. Flip binary tree
Binomial distribution (a discrete distribution)