当前位置:网站首页>LeetCode_ 53 (maximum subarray and)
LeetCode_ 53 (maximum subarray and)
2022-07-01 04:44:00 【***】
Title Description : Give you an array of integers nums , Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and .
A subarray is a continuous part of an array .
Example 1:
Input :nums = [-2,1,-3,4,-1,2,1,-5,4]
Output :6
explain : Continuous subarray [4,-1,2,1] And the biggest , by 6 .
Example 2:
Input :nums = [1]
Output :1
Example 3:
Input :nums = [5,4,-1,7,8]
Output :23
Tips :
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Method 1 : Divide and conquer method ( recursive )
class Solution {
/**
* Transition method
* @param nums Original array
* @return Returns the largest subsegment and
*/
public static int maxSubArray(int[] nums) {
return MaxSubArray(nums,0,nums.length-1);
}
/**
* Find the largest subsegment and
* @param nums Array to be found
* @param left Left pointer
* @param right Right pointer
* @return Maximum sub sum
*/
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){
// Recursion end condition
res=nums[left]; // Recurse until there is only one element left in the sub segment , Then the maximum sum of sub segments is the element
}else{
center=(left+right)/2; // Take the middle element and divide the array into two segments , Divide and rule
leftsum=MaxSubArray(nums,left,center);// Recursively find the sum of the largest sub segments on the left
rightsum=MaxSubArray(nums,center+1,right);// Recursively find the sum of the largest sub segments on the right
sum1=-1230000000;leftsum1=0;// hypothesis sum1 For a very small number , Find the left maximum sub segment sum
for (int i = center; i >=left ; i--) {
leftsum1+=nums[i];
if(leftsum1>sum1)sum1=leftsum1;
}
sum2=-1230000000;rightsum1=0;// hypothesis sum2 For a very small number , Find the sum of the right largest subsegments
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;
}
}
Method 2 、 Dynamic programming
边栏推荐
- Odeint and GPU
- Shell之分析服务器日志命令集锦
- 常用的Transforms中的方法
- 2022危险化学品生产单位安全生产管理人员题库及答案
- Common UNIX Operation and maintenance commands of shell
- STM32扩展版 按键扫描
- Day 52 - tree problem
- [difficult] sqlserver2008r2, can you recover only some files when recovering the database?
- Software testing needs more and more talents. Why do you still not want to take this path?
- 【FTP】FTP连接时出现“227 Entering Passive Mode”的解决方法
猜你喜欢

Simple implementation of slf4j

无器械健身

Dede collection plug-in does not need to write rules

VIM easy to use tutorial

2022年化工自动化控制仪表操作证考试题库及答案

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

The design points of voice dialogue system and the importance of multi round dialogue

Dual contractual learning: text classification via label aware data augmentation reading notes

分布式架构系统拆分原则、需求、微服务拆分步骤

2022 t elevator repair new version test questions and t elevator repair simulation test question bank
随机推荐
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
科研狗可能需要的一些工具
2022年聚合工艺考试题及模拟考试
Maixll dock quick start
STM32扩展板 数码管显示
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
分布式数据库数据一致性的原理、与技术实现方案
2022危险化学品生产单位安全生产管理人员题库及答案
Daily question - line 10
JS image path conversion Base64 format
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
About the transmission pipeline of stage in spark
Seven crimes of counting software R & D Efficiency
Difference between cookie and session
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
Quelques outils dont les chiens scientifiques pourraient avoir besoin
STM32 photoresistor sensor & two channel AD acquisition
Simple implementation of slf4j
Rule method: number of effective triangles