当前位置:网站首页>560 and K
560 and K
2022-07-29 01:39:00 【Java tribute dust collection miraitow】
Give you an array of integers nums And an integer k , Please count and return The array is k The number of consecutive subarrays of .
The prefix and :nums Of the 0 Item to Current item And .
Definition prefixSum Array ,prefixSum[x]: The first 0 Item to The first x term And .
- prefixSum[x] = nums[0] + nums[1] +…+nums[x]
- prefixSum[x]=nums[0]+nums[1]+…+nums[x]
nums An item of = The difference between the sum of two adjacent prefixes :
- nums[x] = prefixSum[x] - prefixSum[x - 1]
- nums[x]=prefixSum[x]−prefixSum[x−1]
nums Of The first i To j term And , Yes :
- nums[i] +…+nums[j]=prefixSum[j] - prefixSum[i - 1]
- nums[i]+…+nums[j]=prefixSum[j]−prefixSum[i−1]
When i by 0, here i-1 by -1, We deliberately let prefixSum[-1] by 0, Make the general formula in i=0 The time is also established :
- nums[0] +…+nums[j]=prefixSum[j]
- nums[0]+…+nums[j]=prefixSum[j]
Equivalent transformation of the topic
There is no need to find prefixSum Array
In fact, we don't care which two prefixes have a difference equal to k, Only care is equal to k The number of occurrences of the difference between the prefix and c, I know there is c The sum of subarrays is equal to k.
Traverse nums Before , We let -1 The corresponding prefix and are 0, This general formula also holds in the boundary case . That is, before traversal ,map Initial placement 0:1 Key value pair ( Prefixes and are 0 appear 1 Time ).
Traverse nums Array , Find the prefix sum of each term , Count the corresponding occurrence times , Store in key value pairs map.
View while saving map, If map in key by 「 The current prefix and - k」, Explain the prefix and , Satisfy 「 The current prefix and - The prefix and == k」, The number of times it appears , Add up count.
Code
Time complexity O(n) . Spatial complexity O(n)
class Solution {
public int subarraySum(int[] nums, int k) {
int pre = 0, count = 0;
Map<Integer,Integer> map = new HashMap<>();
map.put(0,1);
for( int i=0;i<nums.length;i++){
pre += nums[i];
if(map.containsKey(pre-k)){
count += map.get(pre-k);
}
map.put(pre,map.getOrDefault(pre,0)+1);
}
return count;
}
}
边栏推荐
- uniapp movable-view表格缩放过程想保持容器高度不变的解决办法
- Openpyxl border
- 一篇万字博文带你入坑爬虫这条不归路 【万字图文】
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- AlphaFold揭示了蛋白质结构宇宙-从近100万个结构扩展到超过2亿个结构
- uniapp createSelectorQuery(). Select get returns null error
- 全新升级:获得淘宝商品详情“高级版” API
- Letax record \documentclass{}, authoryear attribute is used
- 我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
- Openpyxl cell center
猜你喜欢

20220728 sorting strings that are not pure numbers

【SQL之降龙十八掌】01——亢龙有悔:入门10题

Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device

什么是原码、反码和补码

一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力

Cloud native application comprehensive exercise

Understand various paths

承办首届算力大会,济南胜在何处?

2022年最火的十大测试工具,你掌握了几个
![A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]](/img/aa/a5e7b4516aa395f8d4d0e2eee7d3c7.png)
A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]
随机推荐
一文搞懂ES6基本全部语法
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
CSDN modify column name
AlphaFold揭示了蛋白质结构宇宙-从近100万个结构扩展到超过2亿个结构
DocuWare 移动劳动力解决方案可帮助您构建新的生产力模式:随时随地、任何设备
嵌入式分享合集23
uniapp createSelectorQuery().select 获取返回null报错
Flash reports an error: type object 'news' has no attribute' query 'the view name is duplicate with the model name
[idea] where to use the query field
Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value
【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
[unity project practice] synthetic watermelon
Self-attention neural architecture search for semantic image segmentation
Read the recent trends of okaleido tiger and tap the value and potential behind it
Openpyxl border
Redis is installed on Linux
Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
How to choose professional, safe and high-performance remote control software
Cloud native application comprehensive exercise
[search] - iteration deepening / bidirectional dfs/ida*