当前位置:网站首页>#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.
边栏推荐
- 电商秒杀系统架构设计
- 什么是接口?什么是接口测试?
- CORBA Architecture Guide (Common Object Request Broker Architecture)
- 运动App如何实现端侧后台保活,让运动记录更完整?
- PHP uses stack to solve maze problem
- To be a cross-border e-commerce, you must learn to use PRA software, free your hands and improve efficiency!
- Understanding web automated testing
- 华为云的AI深潜之旅
- Study on bifunctional crosslinker lumiprobe sulfoacyanine 7 dicarboxylic acid
- QJsonObject的使用示例
猜你喜欢

The rogue downloader named by 315 is back
![[software test] 2022 national unified college enrollment examination](/img/9a/d76d7eb30a097d364fef28c2230e1a.png)
[software test] 2022 national unified college enrollment examination

Usage example of qjsonobject

華為雲的AI深潜之旅

AI deep dive of Huawei cloud

Pyechart drawing multiple Y-axis line graphs

华为云的AI深潜之旅

Study on luminiprobe non fluorescent azide -- 3-azido propanol

小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现

什么是接口?什么是接口测试?
随机推荐
Ehcache configuration data, convenient for self checking
How to make up the PMP Exam? How much is the make-up exam?
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
Construction and application of urban brain knowledge map
16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
力扣树的进一步应用
Binary tree problems
Progress in visual weakly supervised learning
LeetCode122. 买卖股票的最佳时机II
PE file-
Lua source code analysis: 1 Lua variable type mutability is implemented in C code.
PAT 1021. Traversal of the deep root (25 points) graph, DFS, calculating the number of connected components
2022年股票在手机上开户安全吗?找谁可以办理?
Pyechart drawing multiple Y-axis line graphs
How to analyze the relationship between enterprise digital transformation and data asset management?
【激活函数】
Understanding web automated testing
Workplace tips | understanding the advantages of the position "knowing people"
How to analyze the relationship between enterprise digital transformation and data asset management?
[software test] 2022 national unified college enrollment examination