当前位置:网站首页>Leetcode 560 prefix and + hash table
Leetcode 560 prefix and + hash table
2022-07-25 10:46:00 【zzh1370894823】
Leetcode 560 The prefix and + Hashtable
Title Description
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 .
Example 1:
Input :nums = [1,1,1], k = 2
Output :2
Thought analysis
Pre knowledge
Prefix and not much introduction
Algorithm ideas
- Add to the current subscript i Prefix of element and prefixSum, namely sum(num[0:i])
- It is required to end with the current element and k The number of , The o i The prefix and before the element are prefixSum - k The number of
- Therefore, the hash table needs to store the prefix and at the end of the element before the current element is x The number of
- Update hash table , Sum the prefix with prefixSum Number of updates , If it didn't exist before value Set to 1, On this basis, existence adds 1. There may have been prefixes and before prefixSum, Because the value of the element may be negative
difficulty : What does the hash table store ?
Add the subscript of the currently traversed element i
The hash table stores :
sum(num[0:1]) The number of
sum(num[0:i2) The number of
sum(num[0:3]) The number of
…
sum(num[0:i-1]) The number of
Code
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int prefixSum = 0;
int count = 0; // Result set
for(int i = 0; i < nums.length; i++){
prefixSum += nums[i];
// Find the sum ending with the current element as k Result , Add to result set
count += map.getOrDefault(prefixSum - k, 0);
// to update map
map.put(prefixSum, map.getOrDefault(prefixSum,0)+1);
}
return count;
}
}
边栏推荐
- 9.shell文本处理三剑客之awk
- 5. This simple "echo" usage, can the child next door!
- Multithreading - static proxy mode
- 2.介绍部署LAMP平台+DISCUZ论坛
- 2021 qunar written examination summary
- Gan, why '𠮷 𠮷'.Length== 3 ??
- 代码的表示学习:CodeBERT及其他相关模型介绍
- 2. Conditional statements of shell script
- 美国机场围棋风格可视化专题图:ArcGIS Pro版本
- Microwave technology homework course design - Discrete capacitance and inductance + microstrip single stub + microstrip double stub
猜你喜欢

VLAN configuration and application (take Huawei ENSP as an example)

Configuration of OSPF protocol (take Huawei ENSP as an example)

2021 scenery written examination summary

电磁场与电磁波实验一 熟悉Matlab软件在电磁场领域的应用

11.iptables 防火墙

思路再次完美验证!加息临近,趋势明了,好好把握这波行情!

Vs Code connects to the remote jupyter server

Introduction to onnx (open neural network exchange)

Using px2rem does not take effect
QT | mouse events and wheel events qmouseevent, qwheelevent
随机推荐
2. Introduce the deployment of lamp platform +discuz Forum
HCIP实验(04)
Acquisition and compilation of UE4 source code
UE4 loadingscreen dynamic loading startup animation
HCIP实验(02)
UE4 window control (maximize minimize)
Several common network diagnostic commands
Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
HCIA实验(09)
UE4 framework introduction
[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22
推荐系统-协同过滤在Spark中的实现
5.这简单的 “echo” 用法隔壁小孩能不会吗!
The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
js 哈希表 02
6. Regular expression of shell
2.shell脚本之条件语句
2021 京东笔试总结
3.信你能理解的!shell脚本之循环语句与函数,数组,冒泡排序
Configuration of static routes (take Huawei ENSP as an example)