当前位置:网站首页>【刷题日记】和为 K 的子数组
【刷题日记】和为 K 的子数组
2022-06-28 16:44:00 【傅耳耳】
和为 K 的子数组
给定一个整数数组和一个整数 k ,请找到该数组中和为 k 的连续子数组的个数。
示例 1:
输入:nums = [1,1,1], k = 2
输出: 2
解释: 此题 [1,1] 与 [1,1] 为两种不同的情况
示例 2:
输入:nums = [1,2,3], k = 3
输出: 2
提示:
1 <= nums.length <= 2 * 10^4-1000 <= nums[i] <= 1000-10^7 <= k <= 10^7
【滑动窗口思维】
- 确定左右边界
- 查找窗口左右滑动的条件
适合求解相关 [连续子数组] 题目
【局限性】但在存在==负数==的数组中不能使用
窗口滑动的条件:
while窗口内元素超过或不满足条件时滑动窗口;若存在负数,则不能确定是移动左边界 or 扩大右边界
思路:前缀和 + 哈希表
适用解题:循环数组的下标N时,需要用到前N-1项的计算结果(和/积)
如何保存?
- 题目明确要求不允许使用额外空间的,直接原地修改数组
- 不限制空间复杂度时,最好额外开辟空间计算,避免数据污染
- 计算时每次只需获取前一次的累计结果 ===> 使用数组存储,每次获取数组末尾元素
- 计算时每次需获取前几次或更多次的结果进行对比 ===> 使用哈希表存储,减小时间复杂度
算法步骤
前缀和差值 = 连续子数组的和
- 找连续子数组的和为K <==> 找
pre[j] - pre[i-1] == k的下标对[i,j] pre[j] - pre[i - 1] == k等价于pre[j] - k == pre[i - 1]- 即当前前缀和 - k 所得为前面某些元素的前缀和
哈希表
hashmap==> 保存已经出现的前缀和 出现的次数,不仅仅是1例:
[-1,1,0], k = 0当遍历到最后一个元素时
presum = 0presum - k = 0,前缀和为0的部分有[-1,1]和[-1,1,0]
不断累加计算当前前缀和
presum,并判断presum - k是否在hashmap中,若存在,结果 + 次数hashmap[presum-k],并保存当前前缀和presum到hashmap中
【边界问题】若nums[0] = k ,但当前 hashmap 为空,不会统计 ==> 初始化 hashmap 为 {0:1}
代码
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
map<int, int> hashmap;
int presum = 0;
int res = 0;
hashmap[0] = 1;
for(int i = 0;i < nums.size();i ++ ) {
presum += nums[i];
// 判断presum-k所得的前缀和是否存在
if(hashmap.find(presum - k) != hashmap.end())
res += hashmap[presum-k] ;
// 保存当前前缀和
hashmap[presum] ++ ;
}
return res;
}
};
时间复杂度:O(n)
空间复杂度:O(n)
边栏推荐
- AUTOSAR software development training
- A simple reflective XSS operation and idea
- 强化 WordPress 的 11 种有效方法
- Use open connector to integrate hubspot and SAP systems
- PotPlayer播放百度云盘视频
- [daily 3 questions (1)] the second largest number in the string
- 如何让你的 WordPress 网站更安全
- 视比特“AI+3D视觉”产品系列 | 上料装配工作站
- 【世界海洋日】TcaplusDB号召你一同保护海洋生物多样性
- 知乎热问:一个程序员的水平能差到什么程度?
猜你喜欢

MATLB|可视化学习(plot和bar)

【TcaplusDB知识库】TcaplusDB技术支持介绍

StackOverflow 2022 开发者报告:PostgreSQL 超越 MySQL !

10.Hystrix断路器

Fs2k face sketch attribute recognition

10.hystrix circuit breaker

The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure

关于接口测试自动化的总结与思考

AutoSAR 软件开发培训

LTspice 电路仿真入门
随机推荐
怎么期货开户?在哪里期货开户比较安全?
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
Written interview algorithm classic - longest palindrome substring
EasyCVR播放视频出现卡顿花屏时如何解决?
使用Karmada实现Helm应用的跨集群部署
[force button] 35 Search insert location
老司机总结的12条 SQL 优化方案(非常实用)
MetaQ安装部署文档
Design details of the full stack CRM development tool webclient UI workbench
2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
Must the database primary key be self incremented? What scenarios do not suggest self augmentation? ByteDance experience sharing using Flink state 𞓜 afternoon tea with sauce issue 16
Noip1998-2018 popularization group csp-j2 2019 2020 problem solving report and video
Gartner announces five privacy trends from today to 2024
GCC efficient graph revolution for joint node representationlearning and clustering
大型体育赛事与犯罪风险
The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
Hello, is it safe to open an account to buy stocks online?
【世界海洋日】TcaplusDB号召你一同保护海洋生物多样性
【TcaplusDB知识库】WebClient用户如何读取和修改数据
Interview with wangyuntao of China Academy of information technology: digital and real integration enables the prosperity and development of cultural industry