当前位置:网站首页>LC:最大子数组和
LC:最大子数组和
2022-06-29 23:24:00 【MyDreamingCode】
题目:给你一个整数数组nums,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
方法一: 暴力法
1. 取 i,j 用于标记位。i 用于记录从哪个位置开始连续,j 用于进行连续。如:
[0,1,2,3,4] i 指向0
j 指向0:0
j 指向1:0,1
j 指向2:0,1,2
j 指向3:0,1,2,3
j 指向4:0,1,2,3,4
2. 取最大值max(初始值设为最小),sum为和值。sum用于计算从 i 到 j 位置中的所有数之和,并与max进行比较,如果大于max,则将sum值赋予max。
3. 遍历数组,直到 i 值为数组中最后一位,计算出sum值,并与max值进行比较。
4. 返回max值。
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define InitSize 20
typedef int ElemType;
typedef struct{
ElemType *data;
int length;
}SqList;
// 初始化顺序表
void initSqList(SqList &L){
ElemType x;
L.length = 0;
L.data = (ElemType*)malloc(sizeof(ElemType)*InitSize);
printf("请输入顺序表(9999为终止):");
scanf("%d",&x);
while(x!=9999){
L.data[L.length]=x;
L.length++;
scanf("%d",&x);
}
}
// 打印顺序表
void printSqList(SqList L){
for(int i=0;i<L.length;++i)
printf("%d ",L.data[i]);
printf("\n");
}
// 最大子数组和
ElemType maxSubArray(SqList L){
ElemType max = INT_MIN; //INT_MIN需要引入limits.h头文件
ElemType sum;
for(int i=0;i<L.length;i++){
sum = 0;
for(int j=i;j<L.length;j++){
sum+=L.data[j];
if(max<sum)
max=sum;
}
}
return max;
}
void main(){
ElemType res;
SqList L;
initSqList(L);
printSqList(L);
res = maxSubArray(L);
printf("最大和为:%d\n",res);
}示例1:

子数组:[5,4,-1,7,8]
示例2:

子数组:[4,-1,2,1]
方法二:动态规划
思想:每一步都是在预设之后的最大值
1. 设置result为最终结果值;
2. 每一步,可以和前面合并,也可以不和前面合并,值放入dp数组中。
3. 式子:
dp[i] = max {dp[i-1] +nums[i] , nums[i]}
result = max {dp[i] , result}
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define InitSize 20
typedef int ElemType;
typedef struct{
ElemType *data;
int length;
}SqList;
// 初始化顺序表
void initSqList(SqList &L){
ElemType x;
L.length = 0;
L.data = (ElemType*)malloc(sizeof(ElemType)*InitSize);
printf("请输入顺序表(9999为终止):");
scanf("%d",&x);
while(x!=9999){
L.data[L.length]=x;
L.length++;
scanf("%d",&x);
}
}
// 打印顺序表
void printSqList(SqList L){
for(int i=0;i<L.length;++i)
printf("%d ",L.data[i]);
printf("\n");
}
//求最大值
ElemType getMax(ElemType a,ElemType b){
return a>b?a:b;
}
// 最大子数组和
ElemType maxSubArray(SqList L){
ElemType dp[InitSize];
ElemType result;
dp[0] = L.data[0];
result = L.data[0];
for(int i=1;i<L.length;i++){
dp[i]=getMax(L.data[i],dp[i-1]+L.data[i]);
result=getMax(dp[i],result);
}
return result;
}
void main(){
ElemType res;
SqList L;
initSqList(L);
printSqList(L);
res = maxSubArray(L);
printf("最大和为:%d\n",res);
}示例同方法一
边栏推荐
- uniapp复制内容到剪贴板
- Effective self summary of remote communication | community essay solicitation
- Gracefully transform the SMS business module and start the strategic mode!
- C指针进阶2-->函数指针数组 回调函数简化计算器代码,基于回调函数模拟实现qsort函数
- Discussion on distributed unique ID generation scheme
- Head on Amway! Good looking and practical motor SolidWorks model material see here
- Wechat applet: picture seconds plus watermark generation
- Why is JSX syntax so popular?
- 剑指 Offer 15. 二进制中1的个数
- Open source the Ernie tiny lightweight technology of "Wenxin big model", which is accurate and fast, with full effect
猜你喜欢

Hematemesis finishing: a rare map of architects!

Discussion on distributed unique ID generation scheme

采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势

论文阅读《Large-Scale Direct SLAM with Stereo Cameras》

The concept and significance of mean, variance, standard deviation and covariance

Halcon实用:焊点检出设计思路

2022 PMP project management examination agile knowledge points (5)

constexpr 函数

Overseas digital authentication service provider advance AI was selected into the "2022 brand sea Service Market Research Report" of equalocean
discrete "digital signal"]"/>Speech signal processing (III): speech signal analysis [continuous "analog signal" -- Sampling, quantization, coding -- > discrete "digital signal"]
随机推荐
Principe de réalisation de l'agent dynamique
Sword finger offer 38 Arrangement of strings
自己收藏的一些网址
Gracefully transform the SMS business module and start the strategic mode!
Project 1 - buffer pool [cmu 15-445645] notes
Solr基础操作4
Regular expressions: characters (2)
redis客户端
新钛云服荣膺“2022爱分析 · IT运维厂商全景报告”云管理平台CMP 代表厂商!...
Discussion on distributed unique ID generation scheme
M1笔记本居家办公的痛点及解决方案 | 社区征文
C指针进阶2-->函数指针数组 回调函数简化计算器代码,基于回调函数模拟实现qsort函数
基于OpenStack的虚拟机在线迁移
Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
简单理解B树和B+树
Solr basic operation 4
剑指 Offer 14- I. 剪绳子
穿越过后,她说多元宇宙真的存在
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
Speech signal processing (III): speech signal analysis [continuous "analog signal" -- Sampling, quantization, coding -- > discrete "digital signal"]