Number of subarrays having sum exactly equal to k(explanation for 437. Path Sum III of leetcode)
https://www.geeksforgeeks.org/number-subarrays-sum-exactly-equal-k/
讲解的详细
看这道题是为了解决https://leetcode.com/problems/path-sum-iii/
import java.util.HashMap;
import java.util.Map; public class test {
static int findSubarraySum(int arr[],int sum){
Map<Integer,Integer> preSum=new HashMap<>();
int res=0,curSum=0;
for(int a:arr) {
curSum+=a;
if(curSum==sum){
res++;
continue;
}
if(preSum.containsKey(curSum-sum)){//如果当前的curSum比sum大,就去找是否之前加过这个差值,那么为了等于sum,我们现在可以知道,只要之前不加这个差值就可以
res+=preSum.get(curSum-sum);
}
preSum.put(curSum,preSum.getOrDefault(curSum,0)+1);
}
return res;
}
public static void main(String[] args) {
int[] arr={10, 2, -2, -20, 10};
System.out.println(findSubarraySum(arr,-10));
}
}
Number of subarrays having sum exactly equal to k(explanation for 437. Path Sum III of leetcode)的更多相关文章
- 47. leetcode 437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- [LeetCode] #112 #113 #437 Path Sum Series
首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- 437. Path Sum III(路径可以任意点开始,任意点结束)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- Bootstrap关闭当前页
function doBack() { var index = parent.layer.getFrameIndex(window.name); parent.lay ...
- DateTimeOffset DateTime
DateTime只保存两部分信息:Ticks和KindTicks 一个Tick是100纳秒(1万Tick等于1毫秒)Ticks记录了从1/1/0001 12:00 AM到现在经过了多少100纳秒.Ki ...
- 学习笔记:Stage.js(又叫Cut.js)——2D canvas 开发库,游戏方面的
http://piqnt.com/stage.js/ http://www.bootcdn.cn/stage.js/ Stage.js(可能又叫Cut.js)是一个2D HTML5 JavaScrip ...
- python练习题_02
#1.有两个列表 l1=[11,22,33] l2=[22,33,44] #a.获取内容相同的元素列表 l3=[] for i in l1: if i in l2: l3.append(i) prin ...
- git push以后GitHub上文件夹灰色 不可点击
1.删除本地文件夹里的 .git .gitignore文件 2.如果没成功,就把文件名改下,应该是有缓存,改完名后再add/commit/push
- ReactNative项目结构目录详解
在使用 react-native init TestProject 在新建项目时,会看到如下目录 React Native结构目录 名称 描述 android目录 Android项目目录,包含了使用A ...
- ArcGIS自定义工具箱-字段合并
ArcGIS自定义工具箱-字段合并 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:用指定字符合并两个字段 用例:湖南/长沙=>湖南省长沙市 数据源: 使 ...
- python re模块与正则表达式
首先要先继承re模块: import re re.findall() 方法 # 返回值为列表 \w 表示一个字符,为数字,字母,下滑线之一, \W匹配任意非数字,字母,下划线 print(re.fin ...
- 4、python常用基础类型介绍
1.字符串 str 描述性质的一种表示状态的例如名字 word='helloworld' print(type(word),word) <class 'str'> helloworld2. ...
- 安装SourceTree工具,无需注册就可以正常使用SourceTree
1. 下载SourceTree安装包 2. 双击安装包进行安装,默认会直接安装在系统盘,此时桌面就会SourceTree的快捷键 3. 双击打开桌面的SourceTree,就会提示让你安装授权,那么接 ...