当前位置:网站首页>Leetcode-560: subarray with sum K
Leetcode-560: subarray with sum K
2022-07-07 10:27:00 【Chrysanthemum headed bat】
leetcode-560: And for K Subarray
subject
Give you an array of integers nums And an integer k , Please count and return The array is 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
Problem solving
Method 1 : The prefix and ( Overtime )
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int n=nums.size();
vector<int> preSums(n+1);
for(int i=0;i<nums.size();i++){
preSums[i+1]=preSums[i]+nums[i];
}
int count=0;
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++){
if(preSums[i]-preSums[j]==k){
count++;
}
}
}
return count;
}
};
Method 2 : The prefix and + Hashtable
The red part is the sum we require k Subarray
If the prefix sum currently traversed is presum, So just know presum-k That's enough , Add presum-k The number of
adopt map To maintain a prefix and The number of
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int,int> map;
map[0]=1;// Prefixes and are 0 The number of 1
int preSum=0;
int count=0;
for(int num:nums){
preSum+=num;
if(map.count(preSum-k)){
// Add prefix and as preSum-k The number of
count+=map[preSum-k];
}
map[preSum]++;
}
return count;
}
};
边栏推荐
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- openinstall与虎扑达成合作,挖掘体育文化产业数据价值
- 1323:【例6.5】活动选择
- Remote meter reading, switching on and off operation command
- JMeter installation
- Multisim--软件相关使用技巧
- 浅谈日志中的返回格式封装格式处理,异常处理
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- Adb 实用命令(网络包、日志、调优相关)
- High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
猜你喜欢
【acwing】786. Number k
LeetCode 练习——113. 路径总和 II
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
XML configuration file parsing and modeling
ArcGIS operation: batch modify attribute table
Methods of adding centerlines and centerlines in SolidWorks drawings
1323:【例6.5】活动选择
Adb 实用命令(网络包、日志、调优相关)
电表远程抄表拉合闸操作命令指令
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
随机推荐
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
使用U2-Net深层网络实现——证件照生成程序
leetcode-303:区域和检索 - 数组不可变
Kotlin realizes wechat interface switching (fragment exercise)
LeetCode 练习——113. 路径总和 II
SolidWorks工程图中添加中心线和中心符号线的办法
STM32 ADC和DMA
OpenGL glLightfv 函数的应用以及光源的相关知识
2022.7.6DAY598
宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
Postman interface test IV
MCU与MPU的区别
Why is the reflection efficiency low?
求最大公约数与最小公倍数(C语言)
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
[email protected] can help us get the log object quickly
ArcGIS operation: converting DWG data to SHP data
Hdu-2196 tree DP learning notes
MCU is the most popular science (ten thousand words summary, worth collecting)
How embedded engineers improve work efficiency