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 ...
随机推荐
- android 开发 View _4_ 我的简单自定义ViewDemo
效果图: 代码: package com.example.lenovo.mydemo.myViewDemo; import android.content.Context; import androi ...
- Git 版本管理使用说明。
1.回滚: git log :查看log日志 commit_id: git reset –-soft <commit_id>:回退到某个版本,只回退了commit的信息,不会恢复到ind ...
- Android下的几种时间格式转换
更多更全的工具类,请参考github上的Blankj/AndroidUtilCode 将毫秒转换为小时:分钟:秒格式 public static String ms2HMS(int _ms){ Str ...
- <记录> curl 封装函数
1. POST请求 参数1 : 请求地址 参数2 : 数组形式的参数 /** * @param string $url post请求地址 * @param array $params * @retur ...
- synchronized 和reentrantlock的优缺点
reentrantlock的优点 可以添加多个检控条件, 如果使用synchronized,则只能使用一个. 使用 reentrant locks 可以有多个wait()/notify() 队列. [ ...
- cut命令详解
1.简介:cut:以某种方式按照文件的行进行分割 2.参数列表: -b:仅显示行中指定直接范围的内容: -c:仅显示行中指定范围的字符: -d:指定字段的分隔符,默认的字段分隔符为“TAB”: -f: ...
- 编译wiredtiger rpm包
1.安装rpm-build 使用rpmbuild打包rpm包前,首先安装rpmbuild: yum install rpm-build -y 2.创建打包文件目录 mkdir -p /root/r ...
- Curl 基本命令
下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://www.centos.org 通过-o/-O选项保存下载的文件到指定的文件中:-o:将文件保存为命令行中指定的文件名 ...
- cisco PBR
access-list 2000 permit ip 10.11.50.0 0.0.0.255 anyaccess-list 2001 permit ip 10.11.50.0 0.0.0.255 1 ...
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...