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

解法一:利用sum之间的差暴力搜索

class Solution {
public int subarraySum(int[] nums, int k) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
int[] sum = new int[len+1];
for (int i=0; i<len; i++)
sum[i+1] = sum[i] + nums[i];
int cnt = 0;
for (int i=0; i<len; i++) {
for (int j=i+1; j<=len; j++) {
if (sum[j] - sum[i] == k)
cnt ++;
}
}
return cnt;
}
}

解法二:利用map建立sum和出现次数cnt的映射关系

class Solution {
public int subarraySum(int[] nums, int k) {
if (nums == null || nums.length == 0)
return 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int sum = 0, cnt = 0;
for (int num : nums) {
sum += num;
cnt += map.getOrDefault(sum-k, 0);
map.put(sum, map.getOrDefault(sum, 0)+1);
}
return cnt;
}
}

第二种解法耗时更少

LeetCode - Subarray sum equals k的更多相关文章

  1. [LeetCode] Subarray Sum Equals K 子数组和为K

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

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

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

  4. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  5. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

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

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

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

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

随机推荐

  1. GMA Round 1 新年祝福

    传送门 新年祝福 15个人聚集在一起,新年到来,他们每个人写下了一句新年祝福.大家把祝福收集起来,然后重新分回去.如果一个人拿到了自己写的祝福,他就会觉得很没有意思,因为得不到别人的祝福.要避免这种尴 ...

  2. 使用C#版Tesseract库

    上一篇介绍了Tesseract库的使用(OCR库Tesseract初探),文末提到了Tesseract是用c/c++开发的,也有C#的开源版本,本篇介绍一下如何使用C#版的Tesseract. C#版 ...

  3. JavaScript常用,继承,原生JavaScript实现classList

    原文链接:http://caibaojian.com/8-javascript-attention.html 基于 Class 的组件最佳实践(Class Based Components) 基于 C ...

  4. Oracle创建Database Link

    一菜单方式: 打开plsql,点击[File]-[New]-[Database link],打开如下图所示窗口 填好各项信息后,点击[Apply]即可完成Database Link的创建. 二SQL方 ...

  5. Netty buffer缓冲区ByteBuf

    Netty buffer缓冲区ByteBuf byte 作为网络传输的基本单位,因此数据在网络中进行传输时需要将数据转换成byte进行传输.netty提供了专门的缓冲区byte生成api ByteBu ...

  6. 经纬度编码方法推荐-plus code简介

    今天罗孚为大家推荐一种经纬度编码的方法--plus code,原名open location code,是Google于2014年发明的,旨在将表示地理位置的经纬度通过算法推导成一个字符串. plus ...

  7. mybatis 批量添加

    <insert id="addTrackBatch" parameterType="java.util.List"> INSERT INTO t_t ...

  8. http重定向到https

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  9. Webhook 实践 —— 自动部署

    https://segmentfault.com/a/1190000007892407 安装nodejs 安装nodejs建议直接下载二进制包,把官网上的64位二进制版本下载地址复制下来,执行 wge ...

  10. VS插件File Nesting

    开发者们一直以来都是使用Visual Studio的解决方案管理器 中的嵌套功能管理项目的子文件夹,使得文件组织清晰.便于访问.鉴于现在的项目巨大的文件数目,如果能将这种嵌入能力应用于项目的其他子项上 ...