当前位置:网站首页>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
边栏推荐
- 图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
- 题解 Ananagrams(UVa156)紫书P113map的应用
- 大智慧上怎么进行开户啊, 安全吗
- ref属性,props配置,mixin混入,插件,scoped样式
- ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
- [learning notes] factor analysis
- 国产数据库名录一览
- MySQL system error occurred 1067
- Ref attribute, props configuration, mixin mixing, plug-in, scoped style
- 数据标准化处理
猜你喜欢

如何使用 DataAnt 监控 Apache APISIX
How to recover after Oracle delete accidentally deletes table data

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

The principle and source code analysis of Lucene index construction

I almost ran away

Pyechart drawing multiple Y-axis line graphs
![[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!](/img/e5/b6035abfa7d4bb59c3080d3b87ce45.jpg)
[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application

Pie (poj3122) super detailed and easy to understand binary introduction

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
随机推荐
mysql-发生系统错误1067
软件watchdog和ANR触发memory dump讲解
什么是接口?什么是接口测试?
GlobalSign的泛域名SSL证书
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
Is it safe to open a dig money account? Is it reliable?
Understanding of incomplete types
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
Analysis of variance
LeetCode121. 买卖股票的最佳时机
[learning notes] cluster analysis
炒股票能赚钱么?开户安全嘛
Please allow the "imperfection" of the current domestic Tob
Leetcode daily question - 522 Longest special sequence II
Bitbucket failed to pull the warehouse Using SSH
如何使用 DataAnt 监控 Apache APISIX
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
LeetCode877. Stone game
LeetCode188. 买卖股票的最佳时机IV
Apisik helps Middle East social software realize localized deployment