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. FTP服务器原理及配置

    控制连接 21端口  用于发送ftp命令 数据连接 20端口  用于上传下载数据 数据连接的建立类型: 1主动模式: 服务器主动发起的数据连接 首先由客户端的21 端口建立ftp控制连接 当需要传输数 ...

  2. Java虚拟机——Class类文件结构

    Class文件格式采用一种类似C语言结构体的结构来存储数据,这种数据结构只有两种数据类型:无符号数和表.      无符号数属于基本的数据类型,数据项的不同长度分别用u1, u2, u4, u8表示, ...

  3. Invalid operator< assertion error解析

    这两天忙着在准备3月份打PAT考试,许久没有接触刷题了,各种生疏各种忘记,刷题速度那是一个慢,真是为自己智商着急.今天刷题碰到了一个有意思的编程习惯性错误,好几道题都涉及到自定义排序,需要自己重写&l ...

  4. MongoDB Compass管理工具下载、安装和使用

    内容来自:https://jingyan.baidu.com/article/925f8cb884f6f8c0dce0565a.html ,https://blog.csdn.net/bg101775 ...

  5. selenium 自动化的坑(3)

    一天一坑系列(3) 今天不讲我是怎么定位了吧,今天讲的是关于弹窗的. 基于业务,一键全否之后需要二次确认,会弹出提示框,你会不会认为这是alert弹框?经过仔细查看元素,确认不是弹框,明明是div嘛, ...

  6. Web自动化-浏览器驱动chromedriver安装方法

    1.python中安装好selenium包  pip install selenium 2.根据以下驱动对照表下载Chrome对驱动   chromedriver版本 支持的Chrome版本 v2.3 ...

  7. Python_010(迭代器)

    一.函数名的运用 1.函数名的内存地址 def func(): print("英雄联盟") print(func) #输出结果: <function func at 0x00 ...

  8. 576 C

    C. MP3 爆ll == #include<bits/stdc++.h> using namespace std; typedef long long ll; #define P pai ...

  9. 破解Revealapp的试用时间限制

    转载自:http://jingwei6.me/2014/02/28/reveal_crack.html Revealapp作为分析iOS app UI结构的利器,还是非常称手的,89刀的价格也是物有所 ...

  10. vue定义组件

    1.定义组件 2.在App.vue里引入Home组件 home代码 <template> <div>home</div> </template> < ...