当前位置:网站首页>LeetCode560. Subarray with and K
LeetCode560. Subarray with and K
2022-06-28 21:06:00 【Yuyy】
Ideas
The first thought is violence , A double loop yields all consecutive substrings , But most of them will be overtime . I didn't think of any other way . Read the explanation of the question , Learned the concept of prefix and , The sum of substrings here is equal to end Prefix and subtract start And . On the basis of prefix and , Combined with the hash To optimize , That is, the sum of two numbers .
There are two areas that need attention . One 、 The required prefix and may appear more than once , So every time you count . Two 、 Prefixes and are 0 It's also a situation , And it is necessary , Need to be manually added . For example, the target is 0.
subject
Given an array of integers and an integer k, You need to find the neutralization of the array as k The number of successive subarrays of .
Example 1 :
Input :nums = [1,1,1], k = 2
Output : 2 , [1,1] And [1,1] For two different situations .explain :
- The length of the array is [1, 20,000].
- The elements in are arrays [-1000, 1000] , And integers k The range is [-1e7, 1e7].
Related Topics
- Array
- Hashtable
- The prefix and
- 1078
- 0
Code
public int subarraySum(int[] nums, int k) {
// key The prefix and ,value Number of occurrences
Map<Integer, Integer> qzh = new HashMap<>(nums.length);
// The length of the substring is 0( At the front of the bus string ), Prefixes and are 0, Number of occurrences +1( Originally 0)
qzh.put(0, 1);
// The prefix and
int sum = 0;
int res = 0;
for (int num : nums) {
sum += num;
// Find the prefix and
final Integer target = qzh.get(sum - k);
if (target != null) {
res += target;
}
// Save the current prefix and the number of occurrences
qzh.put(sum, qzh.getOrDefault(sum, 0) + 1);
}
return res;
}Post Views: 157
边栏推荐
- 开通挖财账号安全吗?是靠谱的吗?
- Lucene构建索引的原理及源代码分析
- LeetCode每日一题——324. 摆动排序 II
- [book club issue 13] packaging format of video files
- openGauss内核分析之查询重写
- LeetCode1114. 按序打印
- 什么是接口?什么是接口测试?
- MySQL system error occurred 1067
- [graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
- LeetCode每日一题——515. 在每个树行中找最大值
猜你喜欢

Leetcode daily question - 515 Find the maximum value in each tree row

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

接口测试流程

Flatten of cnn-lstm
How to recover after Oracle delete accidentally deletes table data

ThreadLocal principle

Leetcode 36. 有效的数独(可以,一次过)

Bitbucket 使用 SSH 拉取仓库失败的问题

APISIX 助力中东社交软件,实现本地化部署

数据标准化处理
随机推荐
LeetCode877. Stone game
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
Stability summary
什么是接口?什么是接口测试?
GlobalSign的泛域名SSL证书
Ehcache配置资料,方便自己查
Leetcode 36. Effective Sudoku (yes, once)
Data standardization processing
How do I download videos? Look at the super simple method!
How to understand the fast iteration of cloud native database?
题解 Pie(POJ3122)超详细易懂的二分入门
postman简介与安装步骤
The further application of Li Kou tree
with torch.no_grad():的使用原因
理解整个网络模型的构建
LeetCode每日一题——522. 最长特殊序列 II
pyechart绘制多条y轴折线图
No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
LeetCode188. The best time to buy and sell stocks IV