当前位置:网站首页>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
边栏推荐
- Kodori tree board
- LeetCode_66(加一)
- 2022 polymerization process test questions and simulation test
- MySQL winter vacation self-study 2022 12 (5)
- 细数软件研发效能的七宗罪
- Leecode question brushing record 1310 subarray XOR query
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- Dataloader的使用
- 扩展-Fragment
- C -- array
猜你喜欢

无器械健身

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点

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

Introduction to JVM stack and heap

STM32扩展版 按键扫描

Dede collection plug-in does not need to write rules

Openresty rewrites the location of 302

STM32 extended key scan

Simple implementation of slf4j
随机推荐
Offline installation of Wireshark 2.6.10
神经网络-最大池化的使用
OdeInt與GPU
【硬十宝典】——1.【基础知识】电源的分类
2022年聚合工艺考试题及模拟考试
[pat (basic level) practice] - [simple simulation] 1064 friends
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
Difficulties in the development of knowledge map & the importance of building industry knowledge map
The design points of voice dialogue system and the importance of multi round dialogue
Common interview questions ①
How to do the performance pressure test of "Health Code"
科研狗可能需要的一些工具
Leecode question brushing record 1332 delete palindrome subsequence
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
1. Mobile terminal touch screen event
扩展-Fragment
LeetCode_58(最后一个单词的长度)
Introduction to JVM stack and heap