当前位置:网站首页>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;
}
};
边栏推荐
- Pdf document signature Guide
- 2022.7.4DAY596
- Methods of adding centerlines and centerlines in SolidWorks drawings
- LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
- XML configuration file parsing and modeling
- 【华为机试真题详解】高矮个子排队
- Study summary of postgraduate entrance examination in September
- Mongodb creates an implicit database as an exercise
- Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
- 【acwing】786. 第k个数
猜你喜欢
Several schemes of building hardware communication technology of Internet of things
对存储过程进行加密和解密(SQL 2008/SQL 2012)
The request object parses the request body and request header parameters
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
ArcGIS operation: converting DWG data to SHP data
Serial communication relay Modbus communication host computer debugging software tool project development case
Programming features of ISP, IAP, ICP, JTAG and SWD
STM32 ADC and DMA
1324:【例6.6】整数区间
ORM model -- creation and query of data records
随机推荐
反射效率为什么低?
ORM -- database addition, deletion, modification and query operation logic
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
[sword finger offer] 42 Stack push in and pop-up sequence
Several schemes of building hardware communication technology of Internet of things
[牛客网刷题 Day6] JZ27 二叉树的镜像
Can I open a stock trading account online? Is it safe
Smart city construction based on GIS 3D visualization technology
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
浅谈日志中的返回格式封装格式处理,异常处理
使用U2-Net深层网络实现——证件照生成程序
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
@Configuration, use, principle and precautions of transmission:
[detailed explanation of Huawei machine test] tall and short people queue up
[email protected]能帮助我们快速拿到日志对象
嵌入式工程师如何提高工作效率
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。