当前位置:网站首页>53. Maximum Subarray最大子数组和
53. Maximum Subarray最大子数组和
2022-07-28 02:34:00 【DXB2021】
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
A subarray is a contiguous part of an array.
子数组 是数组中的一个连续部分。
Example 1:示例 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Example 2:示例 2:
Input: nums = [1]
Output: 1
Example 3:示例 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Constraints:提示:
1 <= nums.length <= 
-
<= nums[i] <= 
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。
C语言(超出时间限制)
int maxSubArray(int* nums, int numsSize){
int max=-10000,temp;
for(int i=0;i<numsSize;i++)
{
temp=nums[i];
if(max<nums[i])
{
max=nums[i];
}
for(int j=i+1;j<numsSize;j++)
{
temp+=nums[j];
if(max<temp)
{
max=temp;
}
}
}
return max;
}C语言:
int maxSubArray(int* nums, int numsSize){
int cur=nums[0];
int max=nums[0];
for(int i=1;i<numsSize;i++)
{
if(cur+nums[i]>nums[i])
{
cur+=nums[i];
}
else
{
cur=nums[i];
}
if(max<cur)
{
max=cur;
}
}
return max;
}执行结果:通过
执行用时:96 ms, 在所有 C 提交中击败了65.95%的用户
内存消耗:11.9 MB, 在所有 C 提交中击败了94.63%的用户
通过测试用例:209 / 209
C语言:
int maxSubArray(int* nums, int numsSize){
int cur=0;
int max=INT_MIN;
for(int i=0;i<numsSize;i++)
{
if(cur<=0)
cur=nums[i];
else
cur+=nums[i];
if(cur>max)
max=cur;
}
return max;
}执行结果:通过
执行用时:88 ms, 在所有 C 提交中击败了97.43%的用户
内存消耗:12.3 MB, 在所有 C 提交中击败了8.30%的用户
通过测试用例:209 / 209
边栏推荐
- Building of APP automation environment (I)
- STM32之IO模拟串口篇
- Stm32f407 ------- FPU learning
- 蓝桥杯:第九届—“彩灯控制器”
- QT topic 1: implementing a simple calculator
- 谈一谈百度 科大讯飞 云知声的语音合成功能
- The test post changes jobs repeatedly, jumping and jumping, and then it disappears
- 【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发2/100
- The applet has obtained the total records and user locations in the database collection. How to use aggregate.geonear to arrange the longitude and latitude from near to far?
- 数据湖:数据库数据迁移工具Sqoop
猜你喜欢

机器人工程是否有红利期

关于权重衰退和丢弃法

43.js -- scope chain

CAD creation group is not combined?

工程电磁场复习基本知识点

QML使用Layout布局时出现大量<Unknown File>: QML QQuickLayoutAttached: Binding loop detected for property循环绑定警告

C#WinForm开发:如何将图片添加到项目资源文件(Resources)中

Day 19 of leetcode

静态博客搭建工具汇总

Full of dry goods, hurry in!!! Easy to master functions in C language
随机推荐
Thread Foundation
社恐适合什么工作?能做自媒体吗?
There is no way to predict the rise and fall of tomorrow
会议OA项目之我的审批&&签字功能
【ACwing 1064 小国王】状压dp
Why is it that when logging in, you clearly use the account information already in the database, but still display "user does not exist"?
【stream】stream流基础知识
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
Original title of Blue Bridge Cup
Actual case of ROS communication
Data Lake: each module component
Embedded sharing collection 22
[email protected]注解使用
JVM 内存布局详解,图文并茂,写得太好了!
小程序已获取数据库合集中的总记录、用户位置,怎么用Aggregate.geoNear将经纬度由近到远排列?
C#WinForm开发:如何将图片添加到项目资源文件(Resources)中
Development and design logic of rtsp/onvif protocol easynvr video platform one click upgrade scheme
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
MySQL索引学习
My approval & signature function of conference OA project