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-treeview初使用
<div id="tree">div> $(function () { function getTree() { var data = [{ text: &quo ...
- window.open() 打开的子页面 往主页面传参问题
<!--主页面的代码--><!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- uni-app开发小程序准备阶段
1.软件安装 开始之前,开发者需先下载安装如下工具: HBuilderX:官方IDE下载地址 下面开发工具根据需求进行安装: 微信小程序开发工具安装 https://developers.weixin ...
- Redis使用及工具类
原地址:https://www.cnblogs.com/wyy123/p/6078593.html [学会安装redis] 从redis.io下载最新版redis-X.Y.Z.tar.gz后解压,然后 ...
- 10. 批量插入List<String>
List<String> iscBusOrgIdList = getIscOrgIdList();List<Map<String, Object>> iscBusO ...
- leetcode437
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- django admin 设置(转载https://www.cnblogs.com/wumingxiaoyao/p/6928297.html)
Django admin 一些有用的设置 Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理数据.后台管理可以在各个app的admin.py文件中进行控制.以下是我最近摸 ...
- jeecg开源项目的IDEA的部署
JEECG采用了SpringMVC + Hibernate + Minidao(类Mybatis) + Easyui(UI库)+ Jquery + Boostrap + Ehcache + Redis ...
- 关于six.with_metaclass(ABCMeta, object)的理解
在学习Python过程中,看到了生成虚基类的方式, class PeopleBase(six.with_metaclass(ABCMeta, object)): @abstractmethod def ...
- hello2
String username = request.getParameter("username");//获取参数值 if (username != null && ...