Leetcode No.53 Maximum Subarray(c++实现)
1. 题目
1.1 英文题目
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
1.2 中文题目
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
1.3输入输出
| 输入 | 输出 |
|---|---|
| nums = [-2,1,-3,4,-1,2,1,-5,4] | 6 |
| nums = [1] | 1 |
| nums = [5,4,-1,7,8] | 23 |
1.4 约束条件
- 1 <= nums.length <= 3 * 104
- -105 <= nums[i] <= 105
2. 实验平台
IDE:VS2019
IDE版本:16.10.1
语言:c++11
3. 程序
3.1 测试程序
#include "Solution.h"
#include <vector> // std::vector
#include<iostream> // std::cout
using namespace std;
// 主程序
void main()
{
// 输入
vector<int> nums = { -100000 };
Solution solution; // 实例化Solution
int k = solution.maxSubArray(nums); // 主功能
// 输出
cout << k << endl;
}
3.2 功能程序
3.2.1 穷举遍历法
(1)代码
#pragma once
#include<vector> // std::vector
using namespace std;
//主功能
class Solution {
public:
int maxSubArray(vector<int>& nums) {
// 暴力求解
int maxValue = -100000;
for (int i = 0; i < nums.size(); i++) //遍历起始值
{
int nowSub = 0;
for (int j = i; j < nums.size(); j++) // 全部遍历一遍
{
nowSub += nums[j];
if (nowSub > maxValue) maxValue = nowSub;
}
}
return maxValue;
}
};
(2)解读
该方法是最容易想到的方法,暴力求解,运用滑动窗口法进行遍历,分别得到以某个为开头的序列进行求最大值,并随遍历的进行实时更新该最大值。复杂度为O(\(n^2\))。
3.2.2 动态规划法
(1)代码
#pragma once
#include<vector> // std::vector
using namespace std;
//主功能
class Solution {
public:
int maxSubArray(vector<int>& nums) {
// 动态规划(时间复杂度O(n),空间复杂度O(n))
int length = nums.size();
vector<int> dp(length); // 存储每次递归的最大值
dp[0] = nums[0];
for (int i = 1; i < length; i++)
dp[i] = max(dp[i - 1] + nums[i], nums[i], [](int a, int b) {return a > b ? a : b; }); // Lamda表达式
//求dp中的最大值
int maxSub = -100000;
for (auto j : dp) // c++11中基于范围的for循环(Range-based for loop)
if (maxSub < j)
dp[j] = maxSub;
return maxSub;
}
};
(2)思路
参考:https://zhuanlan.zhihu.com/p/85188269
3.2.3 kadane算法
(1)代码
#pragma once
#include<vector> // std::vector
using namespace std;
//主功能
class Solution {
public:
int maxSubArray(vector<int>& nums) {
// kadane算法(时间复杂度O(n),空间复杂度O(1))
int length = nums.size();
int maxSub = nums[0]; // 慢指针
int maxSubTemp = nums[0]; //快指针
for (auto i : nums)
{
maxSubTemp = max(maxSubTemp + nums[i], nums[i], [](int a, int b) {return a > b ? a : b; }); // Lamda表达式
if (maxSubTemp > maxSub) // 若当前最大值大于总最大值,则总最大值更新
maxSub = maxSubTemp;
}
return maxSub;
}
};
(2)解读
kadane算法是在动态规划法的基础上加上快慢指针法,快指针指向以i为结尾的子数组最大值之和,慢指针指向迄今为止的子数组最大值之和
3.3.4 分治法(divide and conquer)
(1)代码
pragma once
include // std::vector
//#include<limits.h> // INT_MIN整型最小值
include // std::max
using namespace std;
//主功能
class Solution {
public:
int maxSubArray(vector& nums) {
if (nums.empty()) return 0;
return helper(nums, 0, (int)nums.size() - 1);
}
int helper(vector& nums, int left, int right)
{
if (left >= right) return nums[left];
int mid = left + (right - left) / 2;
int lmax = helper(nums, left, mid - 1);
int rmax = helper(nums, mid + 1, right);
int mmax = nums[mid], t = mmax;
for (int i = mid - 1; i >= left; --i)
{
t += nums[i];
mmax = max(mmax, t);
}
t = mmax;
for (int i = mid + 1; i <= right; ++i)
{
t += nums[i];
mmax = max(mmax, t);
}
return max(mmax, max(lmax, rmax));
}
};
参考:https://www.cnblogs.com/grandyang/p/4377150.html
(2)解读
参考:https://www.jianshu.com/p/3a38d523503b
4. 相关知识
(1)滑动窗口法
滑动窗口其实就是选取部分序列作为窗口,窗口不停移动,直至找到答案,感觉这更像一种思想。
详细介绍可以参考:https://www.cnblogs.com/huansky/p/13488234.html
(2) Lamda表达式
Lamda表达式可以直接在需要调用函数的位置定义短小精悍的函数,而不需要预先定义好函数,但是不便于复用,适用于比较简单且不需要复用的函数。写法为:
func(input1,input2,[],(type1 parameter1,type2 parameter2){函数;})
详细介绍参考:https://blog.csdn.net/A1138474382/article/details/111149792
(3) 基于范围的for循环(Range-based for loop)
c++11中加入的新特性,类似于python,matlab等面向对象语言的for循环,写法为:
for(auto i:array){;}
详细介绍参考:https://blog.csdn.net/hailong0715/article/details/54172848
(4)kadane算法
参考:https://zhuanlan.zhihu.com/p/85188269
Leetcode No.53 Maximum Subarray(c++实现)的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【一天一道LeetCode】#53. Maximum Subarray
一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- LeetCode OJ 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode DP]53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【Leetcode】53. Maximum Subarray
题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- CVPR2019论文看点:自学习Anchor原理
CVPR2019论文看点:自学习Anchor原理 原论文链接:https://arxiv.org/pdf/1901.03278.pdf CVPR2019的一篇对anchor进行优化的论文,主要将原来需 ...
- NSight Compute 用户手册(上)
NSight Compute 用户手册(上) 非交互式配置文件活动 从NVIDIA Nsight Compute启动目标应用程序 启动NVIDIA Nsight Compute时,将出现欢迎页面.单击 ...
- 单核性能强悍,Core i3 这次又要“默秒全”?
单核性能强悍,Core i3 这次又要"默秒全"? 在 Intel 历代酷睿处理器中,定位主流级的 Core i3 一直以超高性价比.低功耗.低发热的特点受到广大用户的青睐,在市场 ...
- 四、配置及使用Zabbix监控系统
要求: 沿用练习- - -,使用Zabbix监控平台监控Linux服务器,实现以下目标:1.监控CPU2.监控内存3.监控进程4.监控网络流量5.监控硬盘 方案:通过Zabbix监控平台,添加被监控z ...
- Java基础面试题(史上最全、持续更新、吐血推荐)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- Java JDK 动态代理(AOP)使用及实现原理分析
一.什么是代理? 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理. 代理模式U ...
- StackOverflow经典问题:代码中如何去掉烦人的“!=null"判空语句
问题 为了避免空指针调用,我们经常会看到这样的语句 if (someobject != null) { someobject.doCalc();} 最终,项目中会存在大量判空代码,多么丑陋繁冗!如何避 ...
- 「10.28」Dove 打扑克(链表)·Cicada 与排序(概率)·Cicada 拿衣服(各种数据结构)
A. Dove 打扑克 考场思考半天线段树树状数组,没有什么想法 打完暴力后突然想到此题用链表实现会很快. 因为只有$n$堆,所以设最多有$x$个不同的堆数,那么$x\times (x-1)/2==n ...
- 安装redHat6.5详细图文教程
进入VM虚拟机,双击进入已经创建好的红帽6虚拟机 双击进入CD/DVD,准备添加红帽6.5的iso镜像文件 [红帽6.5的iso镜像文件需要先下载,redhat_6.5下载地址:https:/ ...
- Ubuntu 之 Esc and CapsLK
Ubuntu 更换 CapsLK 和 ESC 内容如下 // vim .xmodmap !! No Caps Lock clear lock !! Make Caps_lock an escape k ...