当前位置:网站首页>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;
}
}
方法二、动态规划
边栏推荐
- [learn C and fly] S1E20: two dimensional array
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
- Summary of acl2021 information extraction related papers
- Leetcode learning - day 36
- Task04 | statistiques mathématiques
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- Applications and features of VR online exhibition
- The design points of voice dialogue system and the importance of multi round dialogue
猜你喜欢
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
2022年化工自动化控制仪表操作证考试题库及答案
This sideline workload is small, 10-15k, free unlimited massage
神经网络-卷积层
2022危险化学品生产单位安全生产管理人员题库及答案
Shell之一键自动部署Redis任意版本
Obtain detailed ideas for ABCDEF questions of 2022 American Games
2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
2022年上海市安全员C证考试题模拟考试题库及答案
Pytorch(三) —— 函数优化
随机推荐
TASK04|數理統計
Research on medical knowledge atlas question answering system (I)
Web server: how to choose a good web server these five aspects should be paid attention to
Odeint et GPU
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
C - detailed explanation of operators and summary of use cases
[godot] unity's animator is different from Godot's animplayer
What are permissions? What are roles? What are users?
AssertionError assert I.ndim == 4 and I.shape[1] == 3
先有网络模型的使用及修改
Basic exercise of test questions hexadecimal to decimal
pytorch 卷积操作
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
Maixll dock quick start
Pytorch(二) —— 激活函数、损失函数及其梯度
[2020 overview] overview of link prediction based on knowledge map embedding
CUDA development and debugging tool
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
Extension fragment
All in all, the low code still needs to solve these four problems