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 ...
随机推荐
- WEB服务动静结合
基本介绍 1)WEB服务仅能处理静态请求,如果处理动态请求则需要对应的动态资源服务软件,即:应用程序服务软件 2)常见的应用服务软件有:PHP.Java.Python等 3)问题:WEB服务如何与外部 ...
- leetcode 实现-168.Excel表列名称
168.Excel表列名称 描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C … 26 -> Z 27 -&g ...
- Centos 的防火墙(firewalld,iptables)
Centos系统防火墙介绍 概述: 1.Filewalld(动态防火墙)作为redhat7系统中变更对于netfilter内核模块的管理工具: 2.iptables service 管理防火墙规则的模 ...
- vant使用中。。。
微信小程序 没有找到 node_modules 目录 https://blog.csdn.net/u014726163/article/details/82898428 使用有赞的npm包 初始化 n ...
- TFRecords文件的生成和读取(1)
参考:https://blog.csdn.net/u012222949/article/details/72875281 参考:https://blog.csdn.net/chengshuhao199 ...
- 第四周作业—N42-虚怀若谷
一.统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来 [root@centos7 ~]# grep -v "/sbin/nolo ...
- @property属性装饰器
顾名思义,@property就是一个跟属性相关的装饰器, 使用了它之后,取值和赋值操作都变得简洁 from datetime import date, datetime class User: def ...
- php stripos()函数 语法
php stripos()函数 语法 作用:寻找字符串中某字符最先出现的位置,不区分大小写.直线电参数 语法:stripos(string,find,start) 参数: 参数 描述 string ...
- [APIO2013]道路费用
题目描述 幸福国度可以用 N 个城镇(用 1 到 N 编号)构成的集合来描述,这些城镇 最开始由 M 条双向道路(用 1 到 M 编号)连接.城镇 1 是中央城镇.保证一个 人从城镇 1 出发,经过这 ...
- P2258子矩阵
传送 一道看起来就很暴力的题. 这道题不仅暴力,还要用正确的姿势打开暴力. 因为子矩阵的参数有两个,一个行一个列(废话) 我们一次枚举两个参数很容易乱对不对?所以我们先枚举行,再枚举列 枚举完行,列, ...