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:

  1. The length of the array is in range [1, 20,000].
  2. 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

类似题目:

Two Sum

Continuous Subarray Sum

Subarray Product Less Than K

Find Pivot Index

参考资料:

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的更多相关文章

  1. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  2. [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 ...

  3. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  4. LeetCode - Subarray sum equals k

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. 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题是存储的当前累加和的 ...

  9. LeetCode 560. 和为K的子数组(Subarray Sum Equals K)

    题目描述 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] ...

随机推荐

  1. Java 并发编程实践基础 读书笔记: 第三章 使用 JDK 并发包构建程序

    一,JDK并发包实际上就是指java.util.concurrent包里面的那些类和接口等 主要分为以下几类: 1,原子量:2,并发集合:3,同步器:4,可重入锁:5,线程池 二,原子量 原子变量主要 ...

  2. JavaScript 哈希表(散列表)应用

    查找的效率与比较次数密切相关.基于比较的程序,运算效率是比较低的.比如平时可以通过indexOf查找一个数据.但这是一个基于比较的一个实现.如果是淘宝那样有上亿个商品,那么用indeOf 来查数据就会 ...

  3. hibernate框架学习笔记8:一对多关系案例

    两个实体类:客户与联系人,一个客户可以有多个联系人 客户类: package domain; import java.util.HashSet; import java.util.Set; //客户实 ...

  4. Beta预备会议

    1. 讨论组长是否重选的议题和结论. 我们小组决定组长更换为林洋洋同学,他Web开发经验比较丰富,对任务的分配会更加明确,由于上一阶段中存在进度偏慢的问题,我们希望在Beta阶段通过更好的分工安排来保 ...

  5. Hibernate之Hibernate的体系结构

    体系结构简图: 这是一张体系结构的简图,其中的hibernate.properties文件的作用相当于配置文件hibernate.cfg.xml XML Mapping对应的就是映射文件 XXXX.h ...

  6. 前端面试题之css

    1.请列出几个具有继承特性的css属性 font-family  font-size  color  line-height  text-align  text-indent 2.阐述display: ...

  7. 开发者的如何优雅的使用OSX

    Mac对于IT开发者来说是最好的开发工具,没有之一. 但是对于大部分人来说,第一个接触的PC操作系统都是Windows系统,此文将带大家优雅的快速学习和使用Mac的OSX系统. 1. 从键盘说起 Ma ...

  8. MongoDb进阶实践之三 MongoDB查询命令详述

    一.引言           上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命 ...

  9. Mego(05) - 创建模型

    Mego框架使用一组约定来基于CLR类来构建模型.您可以指定其他配置来补充和/或覆盖通过约定发现的内容. 这里需要强调的我们EF不同的是框架只支持数据注释的语法来构建模型,后期只有通过其他接口才能更改 ...

  10. 图数据库orientDB(1-1)SQL基本操作

    SQL基本操作 1.新增VerTex CREATE VERTEX V SET name="user01",sex="M",age="25"; ...