Level:

  Medium

题目描述:

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的连续子数组的个数,求解sum[i ,j]=target的个数,求得sum[0,i]和sum[0,j]就能知道sum[i ,j]。因为我们要求出所有sum(0, i) = sum(0, j) - k的sum(0, i),那么如果有sum(0, i1) = sum(0, i2)的话,可以直接保存一个值sum(0, i)和等于这个值的子数组的个数 count ,然后使用一个 HashMap 保存起来。

代码:

public class Solution{
public int subarraySum(int []nums,int k){
if(nums==null||nums.length==0)
return 0;
int res=0;
HashMap<Integer,Integer>map=new HashMap<>();//键保存sum(0,i),值表示其相同值出现的次数
map.put(0,1);
int sum=0;
for(int i=0;i<nums.length;i++){
sum=sum+nums[i]; //表示sum(0,j)
if(map.containsKey(sum-k)){
res=res+map.get(sum-k);
}
map.put(sum,map.getOrDefault(sum,0)+1);
}
return res;
}
}

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

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

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

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

随机推荐

  1. SSM中前台传数组。后台接受的问题

    当时写得时候,忘记考虑json的jar,做个记录. 第一步:先带入jar <dependency> <groupId>com.fasterxml.jackson.core< ...

  2. 网络基础-IP地址

  3. CSS3 ::before和::after伪元素的实际应用

    实例 1.清除浮动 通常我们清除清除浮动的方式就是在浮动元素后面添加一个空的Div标签,然后在设置它的清除浮动要是,使用after伪元素,我们就不需要添加无意义的div标签在html中了,下面的例子就 ...

  4. 前端每日实战:37# 视频演示如何把握好 transition 和 animation 的时序,创作描边按钮特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/mKdzZM 可交互视频教程 此视频 ...

  5. java匿名内部类 (转载)

    匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:不使用匿名内部类来实现抽象 ...

  6. HashMap底层代码分析

    public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; //this.loadFactor为加载因子,其值为默认的加载因子常量:DEFAUL ...

  7. 阿里云E-HPC联合安世亚太、联科集团共建云超算生态

    5月23日,2018云栖大会武汉峰会,阿里云高级技术专家刘峥和张维,对弹性计算最新上线的 serverless (无服务器化)计算技术Bazaar及基于该技术的容器服务产品 Severless Kub ...

  8. Python全栈开发,Day2

    一.Pycharm的使用 1.创建项目 2.python调整字体大小随ctrl+鼠标滚轮上下滚动 3.python新建程序自动补全编码和环境 4.设置断点(在代码前面行号后面单击鼠标左键) 5.调试断 ...

  9. php prev()函数 语法

    php prev()函数 语法 作用:将内部指针指向数组中的上一个元素,并输出.直线电机选型 语法:prev(array) 参数: 参数 描述 array 必需.指定需要操作的数组. 说明:如果数组包 ...

  10. 2、投资之基金 - IT人思维之投资

    笔者曾经对基金进行投资,但是当时对基金不是很了解,只是为了投资而去投资.现在,笔者对基金的投资有了更深入的了解认识,所以就有了本文. 基金投资在国内还是挺流行的,虽然其是从国外引进来的概念经验,但是国 ...