当前位置:网站首页>#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.
边栏推荐
- Interface use case design
- Go cryptobin common encryption and decryption Libraries
- How to analyze the relationship between enterprise digital transformation and data asset management?
- PHP自学Go日记(四):GO的变量声明方式
- 力扣树的进一步应用
- Biovendor free light chain( κ and λ) Test steps of ELISA Kit
- Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox
- Web 自动化环境搭建
- LeetCode117. 填充每个节点的下一个右侧节点指针_II
- 开通股票炒股账号安全吗?是靠谱的吗?
猜你喜欢

CVPR 2022|极具创意&美感的文字生成方法!支持任意输入

How to analyze the relationship between enterprise digital transformation and data asset management?

Safety innovation practice | Haitai Fangyuan was invited to participate in the technical exchange Seminar on "network information innovation and value co creation in the digital age"

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

The further application of Li Kou tree

ROS 2 Humble Hawksbill 之 f1tenth gym

力扣树的进一步应用

17 `bs object Node name h3 Parent ` parents get parent node ancestor node
![Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function](/img/16/2edfc478a56e5b5e7299621ac778c2.jpg)
Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function

Microsoft's exclusive payment function has also been perfectly unlocked
随机推荐
精通数据分析能力,收入翻倍?什么才是最强竞争力
运动App如何实现端侧后台保活,让运动记录更完整?
炒股票能赚钱么?开户安全嘛
LxC shared USB device
Study on bifunctional crosslinker lumiprobe sulfoacyanine 7 dicarboxylic acid
LeetCode213. 打家劫舍II
Can you make money by speculating in stocks? It's safe to open an account
[width first search note] BFS output shortest path
E-commerce is popular, how to improve the store conversion rate?
ROS 2 Humble Hawksbill 之 f1tenth gym
BOE was brilliant for the Winter Olympics, but revealed another Chinese technology enterprise dominating the world
Activate function
Pat 1024 palindromic number (25 points) sum of large integers
LeetCode123. 买卖股票的最佳时机III
Sword finger offer:[day 1 stack and queue (simple)] --- > use two stacks to realize the queue
Constructing the three-dimensional anti penetration of the actual combat defense system
【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
IPv6 comprehensive experiment
Is the VIP securities account of qiniu school really safe and regular? How do I say this?
Explanation and usage of sqrt() function