当前位置:网站首页>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)
边栏推荐
- 【GoLang】同步锁 Mutex
- For a safer experience, Microsoft announced the first PC with a secure Pluto chip
- Data security is a competitive advantage. How can companies give priority to information security and compliance
- How to choose professional, safe and high-performance remote control software
- The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
- TDA75610-I2C-模拟功放I2C地址的确定
- Autoware reports an error: can't generate global path for start solution
- [WesternCTF2018]shrine
- 【Golang】- runtime.Goexit()
- [hcip] MPLS Foundation
猜你喜欢
![[the road of Exile - Chapter 7]](/img/3c/8b4b7c40367b8b68d0361d9ca4013a.png)
[the road of Exile - Chapter 7]

Talk about possible problems when using transactions (@transactional)

Analyzing the function of human-computer interface module of runtime manager based on autoware

560 and K
![[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area](/img/07/565ca7145bcbef2d467b3c860b7487.png)
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area

Network security litigation risk: four issues that chief information security officers are most concerned about
![[the road of Exile - Chapter 5]](/img/ef/7ecc1cb4a95c613f7be91f7acc761c.png)
[the road of Exile - Chapter 5]

The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities

Basic label in body

【golang】使用select {}
随机推荐
【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
Planning mathematics final exam simulation II
Data security is a competitive advantage. How can companies give priority to information security and compliance
Regular filtering data learning notes (①)
抓包工具Charles使用
How to choose professional, safe and high-performance remote control software
[web technology] 1395 esbuild bundler HMR
Sigma-DSP-OUTPUT
Golang startup error [resolved]
Practical experience of Google cloud spanner
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
数学建模——红酒品质分类
DSP震动座椅
【Golang】- runtime.Goexit()
剑指offer专项突击版第13天
Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
[7.21-26] code source - [good sequence] [social circle] [namonamo]
The basic concept of transaction and the implementation principle of MySQL transaction
Overview of Qualcomm 5g intelligent platform