当前位置:网站首页>[interval and topic prefix and] prefix and + hash table application questions
[interval and topic prefix and] prefix and + hash table application questions
2022-06-21 19:30:00 【Gong Shui Sanye's Diary】
Title Description
This is a LeetCode Upper 「560. And for K Subarray 」, The difficulty is 「 secondary 」.
Tag : 「 The prefix and 」、「 Hashtable 」
Give you an array of integers nums And an integer k , Please count and return the array as k The number of subarrays of .
Example 1:
Input :nums = [1,1,1], k = 2
Output :2
Example 2:
Input :nums = [1,2,3], k = 3
Output :2
Tips :
- 1 <= nums.length <= 2 * 10^4
- -1000 <= nums[i] <= 1000
- -10^7 <= k <= 10^7
The prefix and + Hashtable
This is a classic prefix and application problem .
Statistics with each
For the end , And for
The number of subarrays of is the answer .
We can preprocess prefixes and arrays sum( Prefixes and array subscripts default from
Start ), For solving with a certain
For the end of , And for
Number of subarrays for , In essence, it is to solve in
in ,sum How many values in the array are
Number of numbers , This can be used during traversal 「 Hashtable 」 Synchronous recording .
Code :
class Solution {
public int subarraySum(int[] nums, int k) {
int n = nums.length, ans = 0;
int[] sum = new int[n + 10];
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nums[i - 1];
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int i = 1; i <= n; i++) {
int t = sum[i], d = t - k;
ans += map.getOrDefault(d, 0);
map.put(t, map.getOrDefault(t, 0) + 1);
}
return ans;
}
}
- Time complexity : The complexity of preprocessing prefix and is
, The complexity of the statistical answer is
. The overall complexity is
- Spatial complexity :
Last
This is us. 「 Brush through LeetCode」 The first of the series No.560 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
边栏推荐
- 2022年6月25日PMP考试通关宝典-4
- Post Gartner webinar "nine questions on digital transformation"
- An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
- 昇腾科研创新使能计划赋能开发者&nbsp; 华为计算提供三大维度支持
- 企评家全面解读:【国家电网】中国电力财务有限公司企业成长性
- The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the summary function is used to view the summary statistical information of t
- 鸿蒙版“抖音”,这体验感赞
- Product graphic list description layout style
- Leetcode (210) - Schedule II
- 文献分析 Citespace 6.1.2 下载及安装教程
猜你喜欢

2022年下半年传统产品经理国际资格认证招生简章(NPDP)

Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库

11 introduction and installation of beautiful soup parsing library

Niuke: merging two ordered arrays

JDBC notes

vivo 容器集群监控系统架构与实践

力扣今日题1108. IP 地址无效化

Second cloud's original fully compatible solution for Xinchuang is upgraded to help accelerate the implementation of Xinchuang industry

Nepal graph has settled in Alibaba cloud computing nest to help enterprises build a super large-scale map database on the cloud

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
随机推荐
36 krypton launched | focusing on the innovation of health insurance products, and "Yingshi health" has obtained four rounds of financing
期货开户的流程是什么?网上开户安全吗
Equals null pointer exception
MySQL的MVCC实现原理
Ropsten测试网的水龙头上得到一些ETH
Wwdc22 multimedia feature summary
Is it safe to open futures accounts online? Can I open an account without going offline?
鸿蒙之后,华为宣布再将捐赠欧拉,鸿蒙和欧拉的捐赠预计将给业界带来哪些影响?
50位中国女性科学家入选2022福布斯
R语言dist函数计算dataframe数据中两两样本之间的距离并返回样本间距离矩阵,将距离矩阵输入给hclust函数进行层次聚类分析,method参数指定两个组合数据点间的距离计算方式
WWDC22 多媒体特性汇总
文件上传漏洞靶场分析 UPLOAD_LABS
GetEmptyBlcoksPre Info
南信大2020-2021第一学期FPGA/CPLD期末试卷
Easy introduction to naturallanguageprocessing series topic 6 code practice -- spelling correction based on language model
Cache design issues
根据数据中的key获取value值
【一起上水硕系列】Day One
从“村办企业”到“百亿集团”,红星实业何以完成“蝶变”?
图像分类、AI 与全自动性能测试