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. 关于字符串 --java

    这是在杭电上做一道水题时发现的,挺不错,写下了分享一下 http://acm.hdu.edu.cn/showproblem.php?pid=2072 这里我用了两种方法,参考大佬的,一个是list实现 ...

  2. django实战模拟博客系统

    数据库代码块 from django.db import models from django.utils import timezone from django.contrib.auth.model ...

  3. C_关于递归算法的几个例子

    1.递归算法的定义: 2.递归与迭代的优劣 eg1:斐波那契数列:斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibona ...

  4. 【转】SQL Server 事务隔离级别详解

    SQL 事务隔离级别 概述 隔离级别用于决定如果控制并发用户如何读写数据的操作,同时对性能也有一定的影响作用. 步骤 事务隔离级别通过影响读操作来间接地影响写操作:可以在回话级别上设置事务隔离级别也可 ...

  5. 咏南新CS三层开发框架

    咏南新CS三层开发框架 咏南WEB桌面框架演示:47.106.93.126:9999 咏南WEB手机框架本地:47.106.93.126:8077 咏南CS框架下载:https://pan.baidu ...

  6. Kubernetes中资源配额管理

    设置资源请求数量 创建Pod的时候,可以为每个容器指定资源消耗的限制.Pod的资源请求限制则是Pod中所有容器请求资源的总和. apiVersion: v1 kind: Pod metadata: n ...

  7. I2C总线协议图解

    原帖地址:https://www.cnblogs.com/aaronLinux/p/6218660.html

  8. Cygwin配置总结

    Cygwin配置总结 Cygwin是 大量GNU和开放源码工具的集合,它们提供了类似于Windows上的Linux发行版的功能 DLL(cygwin1.dll),它提供了大量的POSIX API功能. ...

  9. Spring中Mybatis的花样配置 及 原理

    摘自: https://www.jianshu.com/p/fc23c94fc439

  10. numpy.trace对于三维以上array的解析

    numpy.trace是求shape的对角线上的元素的和,具体看 https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.t ...