当前位置:网站首页>leetcode:325. 和等于k的最长子数组长度
leetcode:325. 和等于k的最长子数组长度
2022-08-09 22:01:00 【OceanStar的学习笔记】
题目来源
题目描述
class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k){
}
};
题目解析
思路
- 先生成一个前缀和数组preSum,其长度为N + 1
- 那么,如果存在两个索引 i i i和 j j j,使得 p r e S u m [ j ] − p r e S u m [ i ] = = k preSum[j] - preSum[i] == k preSum[j]−preSum[i]==k,说明找到一个子数组 [ i , j − 1 ] [i, j - 1] [i,j−1],子数组和为k
- 因为要求长度最长,所以定义一个map,其key=preSum[i],value=i。如果存在同样的preSum[i],那么保存i较小的哪一个
- 然后遍历map,找可能的值
class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k){
int N = nums.size();
std::vector<int> preSum(N + 1, 0);
std::map<int, int> map;
preSum[0] = 0;
map[0] = 0;
for (int i = 1; i <= N; ++i) {
preSum[i] = nums[i - 1] + preSum[i - 1];
if(!map.count(preSum[i])){
map[preSum[i]] = i - 1;
}
}
int maxLen = 0;
for (int i = N; i > maxLen; --i) {
if(map.count(preSum[i] - k)){
maxLen = std::max(maxLen, i - map[preSum[i] - k]);
}
}
return maxLen;
}
};
简写
我们不需要另外创建一个累积和的数组,而是直接用一个变量sum边累加边处理,而且我们哈希表也完全不用建立和一维数组的映射,只要保存第一个出现该累积和的位置,后面再出现直接跳过,这样算下来就是最长的子数组
class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k){
int sum = 0, ans = 0;
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
if(sum == k){
ans = i + 1;
}else if(m.count(sum - k)){
ans = std::max(ans, i - m[sum - k]);
}
if(!m.count(sum)){
m[sum] = i;
}
}
return ans;
}
};
测试
int main()
{
Solution solute;
vector<int> nums{
1, -1, 5, -2, 3};
int k=3;
cout<<solute.maxSubArrayLen(nums,k)<<endl;
return 0;
}
类似题目
- algorithm:XX. 和<=k的最长子数组的长度(无序整数数组) 前缀和 + 枚举所有子序列
- leetcode:209. 和>=k的最短子数组的长度(无序正数数组) 前缀和 + 二分
- leetcode:325. 和==k的最长子数组长度(无序整数数组) 前缀和 + 哈希
- leetcode:560. 和== K 的子数组的个数 前缀和 + 哈希
- leetcode:862. 和>=K的最短子数组长度
边栏推荐
- Js fifteen interview questions (with answers)
- The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
- laravel table migration error [easy to understand]
- BulkInsert方法实现批量导入
- R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
- R语言将列表数据转化为向量数据(使用unlist函数将列表数据转化为向量数据)
- Basic JSON usage
- String hashing (2014 SERC J question)
- 一本通2074:【21CSPJ普及组】分糖果(candy)
- SQLi-LABS Page-2 (Adv Injections)
猜你喜欢
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview
一文让你快速了解隐式类型转换【整型提升】!
Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
你真的了解乐观锁和悲观锁吗?
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
华为鸿蒙3.0的野望:技术、应用、生态
[Microservice~Nacos] Nacos service provider and service consumer
xctf攻防世界 Web高手进阶区 shrine
6 rules to sanitize your code
随机推荐
JS–比想象中简单
国内手机厂商曾为它大打出手,如今它却最先垮台……
JS Deobfuscation - AST Restoration Case
A1. Prefix Flip (Easy Version)
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
为什么这么多人都想当产品经理?
跨端技术方案选什么好?
[Microservice~Nacos] Nacos service provider and service consumer
17-GuliMall 搭建虚拟域名访问环境
Basic JSON usage
This article lets you quickly understand implicit type conversion [integral promotion]!
面试官:Redis 大 key 要如何处理?
Bean life cycle
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
【LaTex】 Font “FandolSong-Regular“ does not contain requested(fontspec)Script “CJK“.如何抑制此种警告?
Flutter 绘制美不胜收的光影流动效果
Swift 需求 如何防止把view重复添加到win里面
Rust dereference
你的 Link Button 能让用户选择新页面打开吗?
[Cloud Native] 4.2 DevOps Lectures