当前位置:网站首页>leetcode/和为k的连续子数组个数
leetcode/和为k的连续子数组个数
2022-07-29 01:08:00 【xcrj】
代码
package com.xcrj;
import java.util.HashMap;
import java.util.Map;
/** * 剑指 Offer II 010. 和为 k 的子数组 * 给定一个整数数组和一个整数 k ,请找到该数组中和为 k 的连续子数组的个数。 */
public class Solution10 {
/** * 边界指针,i做j的边界指针 * 0~j~i, j在0到i之间寻找满足条件的连续子数组 * i为j的右边界 * 0为j的左边界 */
public int subarraySum1(int[] nums, int k) {
int number = 0;
// i为j的右边界
for (int i = 0; i < nums.length; i++) {
int sum = 0;
// 0为j的左边界
for (int j = i; j >= 0; j--) {
sum += nums[j];
if (sum == k) number++;
}
}
return number;
}
/** * 前缀和+散列表 * sumPre[j]+k=sumPre[i] * sum(0~j)+sum(j~j+k)=sum(0~i) * 有多少个sumPre[j],就有多少个k。sumPre[j]由sumPre[i]而来 */
public int subarraySum2(int[] nums, int k) {
// 存储<前缀和,次数>
Map<Integer, Integer> map = new HashMap<>(3);
// 前缀为0,0个元素时,只出现1次
map.put(0, 1);
int pre = 0;
int count = 0;
for (int i = 0; i < nums.length; i++) {
pre += nums[i];
if (map.containsKey(pre - k)) {
count += map.get(pre - k);
}
map.put(pre, map.getOrDefault(pre, 0) + 1);
}
return count;
}
public static void main(String[] args) {
Solution10 solution10 = new Solution10();
System.out.println(solution10.subarraySum2(new int[]{
1, 1, 1}, 2));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/QTMn0o/solution/he-wei-k-de-zi-shu-zu-by-leetcode-soluti-1169/
来源:力扣(LeetCode)
边栏推荐
- Autoware reports an error: can't generate global path for start solution
- 九天后我们一起,聚焦音视频、探秘技术新发展
- Minimalist thrift+consumer
- [the road of Exile - Chapter 5]
- Lua third-party byte stream serialization and deserialization module --lpack
- How companies make business decisions -- with the help of data-driven marketing
- 知道创宇上榜CCSIP 2022全景图多个领域
- 活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
- 科研环境对人的影响是很大的
- Data security is a competitive advantage. How can companies give priority to information security and compliance
猜你喜欢
![[the road of Exile - Chapter 4]](/img/76/e1e249ddb2f963abb5d2b617a5f178.png)
[the road of Exile - Chapter 4]
![[the road of Exile - Chapter 2]](/img/98/0a0558dc385141dbb4f97bc0e68b70.png)
[the road of Exile - Chapter 2]

【流放之路-第二章】

Network security litigation risk: four issues that chief information security officers are most concerned about

Stonedb invites you to participate in the open source community monthly meeting!

【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
![[golang] synchronization lock mutex](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[golang] synchronization lock mutex

ciscn 2022 华中赛区 misc

【流放之路-第八章】

Planning mathematics final simulation exam I
随机推荐
【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"
Sword finger offer special assault edition day 13
Reinforcement learning (I): Q-learning, with source code interpretation
Reinforcement learning (II): SARS, with code rewriting
Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?
Nine days later, we are together to focus on the new development of audio and video and mystery technology
【流放之路-第二章】
【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
JVM learning minutes
MPEG音频编码三十年
5g commercial third year: driverless "going up the mountain" and "going to the sea"
StoneDB 邀请您参与开源社区月会!
[机缘参悟-54]:《素书》-1-事物缘起[原始章第一]:大道至简。
九天后我们一起,聚焦音视频、探秘技术新发展
[web technology] 1395 esbuild bundler HMR
规划数学期末模拟考试一
DSP震动座椅
Explanation of yocto project directory structure
Sigma-DSP-OUTPUT