当前位置:网站首页>leetcode剑指offer JZ42 连续子数组的最大和
leetcode剑指offer JZ42 连续子数组的最大和
2022-07-24 05:22:00 【喜乐自在】
此题笔者想出两种 解题模式
第一种暴力解决方法,直接遍历数组array 依次计算a[1]的值 a[1]+a[2]的值 a[1]+a[2]+a[3]的值
依次类推求出 各种连续子数组的值,然后依次与max值比较。
第二种采用动态规划的思想
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
int cursum=0; //cursum为截止到当前数组序号前 连续字数组的最大值
int ret=array[0];//ret为当前数组的第一个字符串
for(int i=0;i<array.size();i++) //遍历数组array
{
if(cursum<=0)
cursum=array[i]; //当前数组序号前最大值小于零,则加上当前序号最大值不超过当前值
else
cursum+=array[i];
ret=max(ret,cursum);//不断将cursum的值进行比较,调出大者放入ret;
}
return ret;
}
};边栏推荐
- IP课总结(3)
- ++cnt1[s1.charAt(i) - ‘a‘];
- Demo of UDP communication applied to various environments
- Unity shader migrated from built-in rendering pipeline to URP
- 配置固定的远程桌面地址【内网穿透、无需公网IP】
- Hololens 2 development: development environment deployment
- 不租服务器,自建个人商业网站(3)
- Using keras to realize LSTM time series prediction based on attention mechanism
- What is monotonic queue
- QT char to qstring hexadecimal and char to hexadecimal integer
猜你喜欢
随机推荐
Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
Jestson installs IBus input method
利用内网穿透,实现公网访问内网
机器学习&深度学习 入门资料分享总结
Lua Foundation
Foundation of JUC concurrent programming (6) -- lock lock
Calculation steps of principal component analysis
Channel attention and spatial attention module
Use QT to connect to MySQL and create table numbers, write data, and delete data
NTP error: no server suitable for synchronization found
使用Keras和LSTM实现对于长期趋势记忆的时间序列预测-LSTNet
Dameng database_ LENGTH_ IN_ Influence of char and charset
如何建立一个仪式感点满的网站,并发布到公网 2-2
unity最新版本的Text(TMP)UI文本怎么显示中文
Installation of tensorflow and pytorch frames and CUDA pit records
Sort ArrayList < ArrayList < double> >
Data warehouse and data warehouse modeling
Getting started with Lunix commands - user and file permissions (Chmod details)
ue4换装系统 1.换装系统的基本原理
【无需公网IP】为远程桌面树莓派配置固定的公网TCP端口地址









