[LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
Note:
- The length of the array is in range [1, 20,000].
- The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
这道题给了我们一个数组,让我们求和为k的连续子数组的个数,博主最开始看到这道题想着肯定要建立累加和数组啊,然后遍历累加和数组的每个数字,首先看其是否为k,是的话结果 res 自增1,然后再加个往前的循环,这样可以快速求出所有的子数组之和,看是否为k,参见代码如下:
解法一:
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = , n = nums.size();
vector<int> sums = nums;
for (int i = ; i < n; ++i) {
sums[i] = sums[i - ] + nums[i];
}
for (int i = ; i < n; ++i) {
if (sums[i] == k) ++res;
for (int j = i - ; j >= ; --j) {
if (sums[i] - sums[j] == k) ++res;
}
}
return res;
}
};
上面的求累加和的方法其实并没有提高程序的执行效率,跟下面这种暴力搜索的解法并没有什么不同,博主很惊奇 OJ 居然这么大度,让这种解法也能通过,参见代码如下:
解法二:
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = , n = nums.size();
for (int i = ; i < n; ++i) {
int sum = nums[i];
if (sum == k) ++res;
for (int j = i + ; j < n; ++j) {
sum += nums[j];
if (sum == k) ++res;
}
}
return res;
}
};
论坛上大家比较推崇的其实是这种解法,用一个哈希表来建立连续子数组之和跟其出现次数之间的映射,初始化要加入 {0,1} 这对映射,这是为啥呢,因为我们的解题思路是遍历数组中的数字,用 sum 来记录到当前位置的累加和,我们建立哈希表的目的是为了让我们可以快速的查找 sum-k 是否存在,即是否有连续子数组的和为 sum-k,如果存在的话,那么和为k的子数组一定也存在,这样当 sum 刚好为k的时候,那么数组从起始到当前位置的这段子数组的和就是k,满足题意,如果哈希表中事先没有 m[0] 项的话,这个符合题意的结果就无法累加到结果 res 中,这就是初始化的用途。上面讲解的内容顺带着也把 for 循环中的内容解释了,这里就不多阐述了,有疑问的童鞋请在评论区留言哈,参见代码如下:
解法三:
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = , sum = , n = nums.size();
unordered_map<int, int> m{{, }};
for (int i = ; i < n; ++i) {
sum += nums[i];
res += m[sum - k];
++m[sum];
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/560
类似题目:
参考资料:
https://leetcode.com/problems/subarray-sum-equals-k/
https://leetcode.com/problems/subarray-sum-equals-k/discuss/102153/Basic-Java-solution
https://leetcode.com/problems/subarray-sum-equals-k/discuss/134689/Three-Approaches-With-Explanation
https://leetcode.com/problems/subarray-sum-equals-k/discuss/102106/Java-Solution-PreSum-%2B-HashMap
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Subarray Sum Equals K 子数组和为K的更多相关文章
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [LeetCode] 560. Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- LeetCode - Subarray sum equals k
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- LeetCode 560. 和为K的子数组(Subarray Sum Equals K)
题目描述 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] ...
随机推荐
- 【备忘】Idea的那些事
说到Java的IDE,似乎eclipse和Idea是目前的主流.然而,OO的课程组却一直在推荐使用eclipse,于是很多人就这样错过了Idea这样强大的IDE工具.本文将会对于Idea和Idea的一 ...
- JavaScript(第二十二天)【动态加载js和css】
学习要点: 1.元素位置 2.动态脚本 3.动态样式 本章主要讲解上一章剩余的获取位置的DOM方法.动态加载脚本和样式. 一.元素位置 上一章已经通过几组属性可以获取元素所需的位置,那么这节课补充 ...
- 学号:201621123032 《Java程序设计》第13周学习总结
1:本周学习总结 2:为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 2.1:简述你想为你的系统增加什么网络功能?设计思路是什么? 创建了一个服务器监听8080端口,通过网络助手客户 ...
- Beta冲刺Day2
项目进展 李明皇 今天解决的进度 优化了信息详情页的布局:日期显示,添加举报按钮等 优化了程序的数据传递逻辑 明天安排 程序运行逻辑的完善 林翔 今天解决的进度 实现微信端消息发布的插入数据库 明天安 ...
- Flask 学习 七 用户认证
使用werkzeug 实现密码散列 from werkzeug.security import generate_password_hash,check_password_hash class Use ...
- EasyUI导航栏。
html: <div data-options="region:'west',split:true" title="导航栏菜单" style=" ...
- Linq 等式运算符:SequenceEqual
检查元素的数量,每个元素的值及两个集合中元素的顺序是否相等,3个方面都相等则为true,否则为false IList<string> strList1 = new List<stri ...
- PHP常见排序算法
$a = [1, 3, 5, 2, 4, 6, 12, 60, 45, 10, 32];$len = count($a);$num=0;/* * 冒泡排序 * 原理:不停的对相邻两个数进行比较,直到最 ...
- VMware-vCenter-Server-Appliance VCSA升级步骤
1.下载ZIP升级文件并解压 2.打开HFS,把解压后的文件夹拖到"Virtual File System"下,在弹出的对话框中点击"Virtual folder&quo ...
- jQuery兼容浏览器IE8方法
在维护公司网站的时候,发现在IE8下jquery会报对象不支持此属性或方法.缺少对象的错误: 在其他浏览器就可以正常运行,当前使用的jquery版本是3.1.1,查资料发现jquery从2.0开始不 ...