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 ...
随机推荐
- windows的cmd批处理命令及powershell (二)
1.变量设置 for /l %%i in (1,1,100) do @echo %%i set /a i=500set /a i=%i%+200echo %i%pause ++++++++++++++ ...
- Linux输入子系统详解
input输入子系统框架 linux输入子系统(linux input subsystem)从上到下由三层实现,分别为:输入子系统事件处理层(EventHandler).输入子系统核心层(Input ...
- ---command line edit and histroy of perl cpan shell
https://www.redhat.com/archives/psyche-list/2003-February/msg00494.html perl -MCPAN -e shell > in ...
- IVideoWindow 在directshow采集链路中的使用
dshow中一个完整采集链路一般如下: Capture Device----->SampleGraber ------>Render 如果只要采集原始数据可以不用渲染链路那就如下: Cap ...
- css选择器querySelectorAll
* querySelectorAll(css的选择器)* 通过css的选择器获取到的一组元素* 获取的也是类数组** 主语* document 从整个页面去获取一组元素* 父级 从父级下去获取一组元素 ...
- 数据库中多对多关系的处理 User---Role
--一个用户可以担任多个角色,如user1既是调度员又是分拣员--一个角色可以被多个用户担任,如user1是调度员,user2也是调度员--用户和角色之间的对应关系为多对多,所以会产生中间表 t_us ...
- 4、python常用基础类型介绍
1.字符串 str 描述性质的一种表示状态的例如名字 word='helloworld' print(type(word),word) <class 'str'> helloworld2. ...
- eclipse git(版本回退)
https://www.cnblogs.com/duex/p/6389999.html
- Python设计模式 - UML - 组件图(Component Diagram)
简介 组件图又称构建图,用于显示系统各组件及各组件关系的物理视图. 组件图通常包括组件.接口.关系.端口和连接器,用来显示程序代码中相应的模块.源文件或源文件集合之间的依赖和泛化关系. 组件图中的组件 ...
- http://ctf.bugku.com/challenges#%E9%80%86%E5%90%91%E5%85%A5%E9%97%A8:bugku--逆向入门
文件是: 分析挺简单,主要是data urls知识点. 首先使用peid检测是否加壳,发现它居然是jpg文件.使用notepad++查看,结果如下. 嗯,百度一下子,知道了data ...