当前位置:网站首页>【刷题日记】和为 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)
边栏推荐
- [tcapulusdb knowledge base] view the business password
- Js中的Bom
- [daily 3 questions (1)] the second largest number in the string
- 基于DataWorks的时效仿真平台|得物技术
- 【TcaplusDB知识库】查看业务密码
- 【每日3题(1)】字符串中第二大的数字
- 这个简单的小功能,半年为我们产研团队省下213个小时
- Five solutions that give consideration to enterprise anti epidemic and development, from IBM
- LDD knowledge sorting
- 解决sqoop出现 ERROR manager.SqlManager: Generic SqlManager.listDatabases() not implemented
猜你喜欢

Potplayer plays Baidu cloud disk video

Use open connector to integrate hubspot and SAP systems

Tianyi cloud web application firewall (edge cloud version) passed the first batch of trusted authentication

知乎热问:一个程序员的水平能差到什么程度?

【TcaplusDB知识库】TcaplusDB限制条件介绍

Csp-j1 csp-s1 preliminary training plan and learning points in summer and September 2022

Cross cluster deployment of helm applications using karmada

这个简单的小功能,半年为我们产研团队省下213个小时

解决sqoop出现 ERROR manager.SqlManager: Generic SqlManager.listDatabases() not implemented

大型体育赛事与犯罪风险
随机推荐
Langqing and Langchao, an ecological model from OEM to value symbiosis
Please ask me, the queries written in my database account for 99%. Is it better to use pay as you go mode or reservation mode?
【每日3题(2)】最大升序子数组和
中金证券经理给的开户链接安全吗?找谁可以开户啊?
MetaQ安装部署文档
【尚硅谷与腾讯云官方合作】硅谷课堂项目视频发布
【TcaplusDB知识库】查看tcapdir目录服务器
如何让你的 WordPress 网站更安全
How to query the last data according to multiple indexes to achieve the effect of SQL order by desc limit 1?
【Hot100】1. Sum of two numbers
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
【Hot100】3. Longest substring without duplicate characters
How to install WordPress on a web site
Inspur network wins step by step
Can Huawei become a "brother of lipstick" or a "Queen of goods"?
批量修改指定字符文件名 bat脚本
[laravel] about the composer installation of laravel8
[daily 3 questions (2)] maximum ascending subarray sum
Curve 替换 Ceph 在网易云音乐的实践
PostgreSQL exception handling