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. ISDN简记

    简介 ISDN:(Integrated Services Digital Network,综合业务数字网) 是以综合数字电话网(IDN)为基础发展演变而形成的通信网,能够提供端到端的数字连接,用来支持 ...

  2. IO流(6)—转换流

    1.处理流之二:转换流 InputStreamReader和OutputStreamWriter 2.当作用的文件就是一个文本文件且使用字节流传输时,需要把它转换成字符流,再在外面加上缓冲流以加速传输 ...

  3. C# RabbitMQ优先级队列实战项目演练

    一.需求背景 当用户在商城上进行下单支付,针对客户等级的不同和订单金额的大小划分客户级别,需要优先处理给标识为大订单的客户发送一份订单邮件提醒.那么我们应用程序如何解决这样的需求场景呢?今天阿笨给大家 ...

  4. line-height 设置为 1

    https://stackoverflow.com/questions/1000398/what-is-line-height1   If no unit is supplied e.g. " ...

  5. 近期 Unity 提交苹果审核被拒的问题

    游戏提交苹果审核,被打回.在 bugly 上没有查到崩溃信息,苹果给了 crash 日志也说明. 拒绝原因如下: Your app crashed on iPad or iPhone running ...

  6. C# Task 是什么?返回值如何实现? Wait如何实现

    关于Task的API太多了,网上的实例也很多,现在我们来说说Task究竟是个什么东西[task一般用于多线程,它一定与线程有关],还有它的返回值有事怎么搞的. 首先我们以一个最简单的API开始,Tas ...

  7. Centos7 设置、查看、添加、删除服务的开机启动项

    查看开机启动项 systemctl list-unit-files |   grep enable 为服务添加开机启动项 systemctl enable zabbix-server.service ...

  8. PHP异步扩展Swoole笔记(1)

    安装Swoole扩展 通过pecl安装, 系统中最好已经有http2依赖, 如果是Ubuntu, 可以直接通过apt安装nghttp2, 如果是Centos或者需要自己编译, 在Github下载ngh ...

  9. Mongodb系列- spring-data-mongodb使用MongoTemplate实现分页查询

    在用spring-data-mongodb框架开发的过程中,需要实现分页查询,就百度了下,没找到满意的又google了下,找到了思路. 在spring-data-mongodb 官方文档中,建议你使用 ...

  10. Vue $emit()不触发方法的原因

    vue使用$emit时,父组件无法触发监听事件的原因是: $emit传入的事件名称只能使用小写,不能使用大写的驼峰规则命名