当前位置:网站首页>LeetCode_53(最大子数组和)
LeetCode_53(最大子数组和)
2022-07-01 04:37:00 【***】
题目描述: 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组是数组中的一个连续部分。
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
提示:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
方法一:分治法(递归)
class Solution {
/**
* 过渡方法
* @param nums 原数组
* @return 返回最大子段和
*/
public static int maxSubArray(int[] nums) {
return MaxSubArray(nums,0,nums.length-1);
}
/**
* 求最大子段和
* @param nums 待求数组
* @param left 左指针
* @param right 右指针
* @return 最大子段和
*/
public static int MaxSubArray(int[] nums,int left,int right) {
int res=0,leftsum,rightsum,midsum,center;
int sum1,sum2,leftsum1,rightsum1;
if(left==right){
//递归结束条件
res=nums[left]; //递归直至子段只剩一个元素,则最大子段和即为该元素
}else{
center=(left+right)/2; //取中间元素将数组分为两段,分而治之
leftsum=MaxSubArray(nums,left,center);//递归求左边的最大子段和
rightsum=MaxSubArray(nums,center+1,right);//递归求右边的最大子段和
sum1=-1230000000;leftsum1=0;//假设sum1为一个很小的数,求左最大子段和
for (int i = center; i >=left ; i--) {
leftsum1+=nums[i];
if(leftsum1>sum1)sum1=leftsum1;
}
sum2=-1230000000;rightsum1=0;//假设sum2为一个很小的数,求右最大子段和
for (int i = center+1; i <=right ; i++) {
rightsum1+=nums[i];
if(rightsum1>sum2)sum2=rightsum1;
}
midsum=sum1+sum2;
int s;
if(leftsum>rightsum)s=leftsum;
else s=rightsum;
if(s>midsum)res=s;
else res=midsum;
}
return res;
}
}
方法二、动态规划
边栏推荐
- Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
- Extension fragment
- 2022 gas examination question bank and online simulation examination
- JS image path conversion Base64 format
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- Common UNIX Operation and maintenance commands of shell
- Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
- Offline installation of Wireshark 2.6.10
- 1. Mobile terminal touch screen event
- Rule method: number of effective triangles
猜你喜欢

One click shell to automatically deploy any version of redis

(12) Somersault cloud case (navigation bar highlights follow)

Obtain detailed ideas for ABCDEF questions of 2022 American Games

VIM简易使用教程

神经网络-使用Sequential搭建神经网络

Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)

Dede collection plug-in does not need to write rules

2022 question bank and answers for safety production management personnel of hazardous chemical production units

神经网络-卷积层
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends
随机推荐
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
C language games (I) -- guessing games
JS rotation chart
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
JVM栈和堆简介
Some small knowledge points
The design points of voice dialogue system and the importance of multi round dialogue
2022 gas examination question bank and online simulation examination
Introduction to JVM stack and heap
Shell analysis server log command collection
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
Leecode question brushing record 1310 subarray XOR query
Pytorch(二) —— 激活函数、损失函数及其梯度
Offline installation of Wireshark 2.6.10
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
OdeInt與GPU
Maixll-Dock 使用方法