当前位置:网站首页>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;
}
}
方法二、动态规划
边栏推荐
- C - detailed explanation of operators and summary of use cases
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- 2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
- Programs and processes, process management, foreground and background processes
- 测量三相永磁同步电机的交轴直轴电感
- selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
- [pat (basic level) practice] - [simple simulation] 1064 friends
- Some small knowledge points
- Applications and features of VR online exhibition
- 2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
猜你喜欢

Ten wastes of software research and development: the other side of research and development efficiency

【硬十宝典】——1.【基础知识】电源的分类

2022年煤气考试题库及在线模拟考试

Task04 | statistiques mathématiques

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

C - detailed explanation of operators and summary of use cases

VR线上展览所具备应用及特色

Grey correlation cases and codes

TCP server communication flow
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends
随机推荐
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
C -- array
RDF query language SPARQL
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
Odeint et GPU
(12) Somersault cloud case (navigation bar highlights follow)
Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
Maixll dock quick start
2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
2. Use of classlist (element class name)
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
How to use maixll dock
Applications and features of VR online exhibition
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
2022 gas examination question bank and online simulation examination
Kodori tree board
OdeInt与GPU
What is uid? What is auth? What is a verifier?
Leecode records the number of good segmentation of 1525 strings