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)的更多相关文章

  1. 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 ...

  2. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  3. 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 ...

  4. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  5. [LeetCode] #112 #113 #437 Path Sum Series

    首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...

  6. 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 ...

  7. [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 ...

  8. 437. Path Sum III(路径可以任意点开始,任意点结束)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [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 ...

随机推荐

  1. jmeter单sql语句测试

    前提:在进行接口或者性能测试时需要用到数据库连接,此文讲解简单的单sql语句执行 步骤1:启动jmeter,新建一个测试计划,新建一个Thread(此处不作详细说明) 步骤2:再新建一个JDBC Co ...

  2. win10 linux 子系统 所在 目录

    C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\r ...

  3. 60.纯 CSS 创作一块乐高积木

    原文地址:https://segmentfault.com/a/1190000015369542 感想:y轴旋转,相对定位,今天有点懵呀,唉. HTML code: <!-- 定义dom,容器中 ...

  4. C#进阶系列——AOP

    一.AOP概念(转自) 老规矩,还是先看官方解释:AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程 ...

  5. 4-29 c语言之栈,队列,双向链表

    今天学习了数据结构中栈,队列的知识 相对于单链表来说,栈和队列就是添加的方式不同,队列就相当于排队,先排队的先出来(FIFO),而栈就相当于弹夹,先压进去的子弹后出来(FILO). 首先看一下栈(St ...

  6. Django01-Django基础

    一.什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统.对于所有的Web应用,本质上其实就是一个socket服 ...

  7. K8s之Etcd

    Etcd是一个开源的.高可用的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现.etcd与zookeeper相比算是轻量级系统,两者的一致性协议也一样,etcd的raft比zookeepe ...

  8. 37.Spring-事务控制.md

    目录 1.分类 2.Spring对jadc事务管理 2.1xml方式 2.1.1首先定义Dao对象和Server对象 2.1.2配置文件实现事务管理 2.2注解方式 2.2.1对象类 2.2.2配置文 ...

  9. Hibernate 再接触 性能优化

    Sessionclear 否则session缓存里越来越多 Java有内存泄露吗? 在语法中没有(垃圾自动回收) 但是在实际中会有 比如读文件没有关什么的 1+N问题 解决方法:把fetch设置为la ...

  10. window.location.href 页面不跳转解决

    function login() { var userid = $("#username").val(); var userpwd = $("#pwd").va ...