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. flask之flask-script组件

    Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...

  2. JSON序列——保存修改数据

    JSON序列——保存修改数据 procedure TForm1.Button6Click(Sender: TObject); begin var delta: TynJsonDelta := TynJ ...

  3. Mac上搭建ELK

    转载自我的个人博客:http://blog.ywheel.cn/post/2017/03/04/setup_elk_on_mac/ 最近的项目需要对文本数据各字段进行快速检索.组合查询.模糊查询,在架 ...

  4. 微信小程序判断用户是否需要再次授权获取个人信息

    一.index.js设置如下 //获取用户的授权信息,放到本地缓存中 wx.getSetting({ success: (res) => { if(res.authSetting['scope. ...

  5. Can't connect to MySQL server (10065)

    在一台机器上连接另一台机器的MySQL服务器时,出现了下面的错误: Can't connect to MySQL server (10065) 这是对方机器没有关闭防火墙造成的,关闭对方机器防火墙以后 ...

  6. 每天一个linux命令:vmstat

    1.命令简介 vmstat(Virtual Memory Statistics 虚拟内存统计) 命令用来显示Linux系统虚拟内存状态,也可以报告关于进程.内存.I/O等系统整体运行状态. 2.用法 ...

  7. GopherChina 2018

    https://github.com/gopherchina/conference/tree/master/2018

  8. Git-Book

    关于git的文档https://git-scm.com/book/zh/v2

  9. APP开发的基本流程

    一个独立App开发人的自白:做APP就是一场赌局,你要会押注 下面我们就直接来看下APP从业者必知的整个APP开发标准流程. 一般的APP开发及上线流程 步骤如下: 首先,制作一款APP,必须要有相关 ...

  10. python 模块学习

    一.from django.contrib.auth.hashers import make_password 通过函数名即可发现,主要有两个函数,分别是创建密码和验证 用法 ps = " ...