66.Subarray Sum Equals K(子数组和为K的个数)
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:
- The length of the array is in range [1, 20,000].
- 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的个数)的更多相关文章
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [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 ...
- [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 ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- 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 ...
- [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 ...
- [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 ...
- 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题是存储的当前累加和的 ...
- 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 ...
随机推荐
- 08-求解Ax=b:可解性和解的结构
一.增广矩阵 假设我们要求解方程$Ax=b$,其中矩阵$A$和$b$如下所示: $A = \left[\begin{array}{llll}{1} & {2} & {2} & ...
- gbase整合mybatis出现: Cause: java.sql.SQLException: Can't convert to: binary stream
参考地址:http://mybatis-user.963551.n3.nabble.com/Map-SQL-Type-LVARCHAR-x-to-JDBC-Type-VARCHAR-globally- ...
- idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.
ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...
- alert(1) to win 15
- 美国的科技公司是如何使用加密的DNS
加密设备和“以隐私为中心”的提供商之间的DNS流量可以阻止某人窥探您的浏览器所指向的位置,或者使用DNS攻击将其发送到其他地方. 该网络中立性的死亡和法规对互联网服务供应商如何处理客户的网络流量的松动 ...
- python NameError: name 'file' is not defined
import sys import time import os poem='''\ 测试读写文件 ''' print(os.getcwd()) f=file(os.getcwd()+'/python ...
- powerdesign 显示comment,生成excel
一.显示 Comment 1. 在弹窗的Table描述里面显Comment 效果图 实现方式: 2. 在最外层显示Comment 效果图 现实方式: 2.1. Tools > Display P ...
- netty模型简介
Netty工作原理图 netty抽象出了两组线程池,BossGroup专门负责客户端 的连接,WorkerGroup专门负责网络读写. BossGroup和WorkerGroup 类型都是NioEve ...
- 20180910-Java 文档注释
Java 文档注释 Java只是三种注释方式.前两种分别是// 和/* */,第三种被称作说明注释,它以/** 开始,以 */结束. // /* */ /** */ 说明注释允许你在程序中嵌入关于程序 ...
- NOIp 基础数论知识点总结
推荐阅读 NOIp 数学知识点总结: https://www.cnblogs.com/greyqz/p/maths.html Basic 常用素数表:https://www.cnblogs.com/g ...