当前位置:网站首页>Clearance sword refers to Offer——The sword refers to Offer II 010. and the sub-array of k
Clearance sword refers to Offer——The sword refers to Offer II 010. and the sub-array of k
2022-08-04 17:03:00 【SK_Jaco】
1.题目描述
给定一个整数数组和一个整数 k ,请找到该数组中和为 k 的连续子数组的个数.
示例 1:
输入:nums = [1,1,1], k = 2
输出: 2
解释: 此题 [1,1] 与 [1,1] 为两种不同的情况
示例 2:
输入:nums = [1,2,3], k = 3
输出: 2
2.解题思路与代码
2.1 解题思路
This problem requires summing up contiguous subarrays of a number,Then the problem of summing continuous subarrays like this can first be solved by using prefix sum.We denote prefixes and arrays as sum[] The original array is recorded as num[],Prefixes and arrays have the following two properties
- 前缀和数组第 i The bit value is the original array [0, i] The sum of all elements in bits
- sum[i]-sum[j] (i>j) equal to the original array [i+1, j] 上元素之和
基于这两个特性,We can know the contiguous subarray sum required by the title k,In fact, it is to find whether there are two positions on the prefix and the array i 和 j (其中 j>i)使得 sum[j]-sum[i]=k,After this is deduced, the problem is very simple.
We have the above formula sum[j]-sum[i]=k 进行变形得到 sum[j]-k=sum[i],When we get the prefix and the array, we traverse the original array from the beginning to the end num[],And the meaning of this deformed formula is when we find the first j The prefix and hour of the location,在 j Whether there is a prefix and equals to the first j Prefix and subtract of positions k,每存在一个 sum[i] 等于 sum[j]-k Add the statistics 1.由于需要在 j Look up the prefix sum before,To reduce traversal we use a hash table to store the number of prefixes and counts previously computed.这里有一点需要注意,The prefix sum array requires one more bit than the original array,And the prefix sums the first of the array 0 位置为 0.
以题目示例 nums = [1,1,1], k = 2 为例进行图解.
First we initialize the prefix sum array and hash table,The prefix and array bits are one more than the original array to store the initial 0,Indicates that the prefix sum is yes when no number is selected at this time 0,并将 0 放入哈希表中
Start calculating the prefix sum,Traverse the first position of the original array,此时 sum[1] 等于 1,Then according to the previous formula,We need to find the first 1 Whether there is a prefix and equals in front of the bitsum[1]-k 即 -1,It was not found in the hash table,于是将 sum[1] Continue to traverse after putting into the hash table
Continue to calculate the prefix sum,此时 sum[2] 等于 2,Then need to see sum[2] Whether there is a prefix and equals before sum[2]-2 即 0,can be obtained from the hash table 0 出现了一次,统计结果加一,并将 sum[2] 放入哈希表中
Finally traverse to the first of the original array 2 位,此时 sum[3] 等于 3,Then you need to find whether it exists in the previous prefix sum sum[3]-2 即 1,在哈希表中存在 1,and only appeared once,So the result is incremented by one,最终得到结果是 2.
2.2 代码
class Solution {
public int subarraySum(int[] nums, int k) {
if (nums.length == 1) {
return nums[0] == k ? 1 : 0;
}
// 初始化前缀和数组,The length is one bit more than the original array,并且第 0 位设置为 0
int[] sum = new int[nums.length + 1];
sum[0] = 0;
// Initialize the hash table storage
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int ans = 0;
for (int i = 1; i < nums.length + 1; i++) {
// 计算前缀和,and calculate what needs to be looked up target
sum[i] = sum[i - 1] + nums[i - 1];
int target = sum[i] - k;
// Read from hash table target included in the results
ans += map.getOrDefault(target, 0);
// Store the prefix sum of the current bit into the hash table
map.put(sum[i], map.getOrDefault(sum[i], 0) + 1);
}
return ans;
}
}
2.3 测试结果
通过测试
3.总结
- Solve using prefix sum and hash table
- Prefixes and arrays need to be initialized to use 0 占位
边栏推荐
- Cesium快速上手0-Cesium安装与基本介绍
- 码蹄集 - MT2165 - 小码哥的抽卡之旅1
- 安装win11提示开启安全模式如何解决
- 湖北电信天邑TY1608_S905L3B_MT7668_卡刷固件包
- Heilongjiang Mobile New Magic Hundred Box M411A_2+8_S905L3A_wire brush firmware package
- dotnet core 隐藏控制台
- Win10 无线网卡驱动感叹号,显示错误代码56
- 移动CM101s_MV100_EMMC_M8233_强刷后全分区线刷固件包
- 机器学习(十八):随机搜索和XGBoost
- 移动平台助力推进智慧型科研院所信息化建设
猜你喜欢
随机推荐
移动魔百盒CM201-1_CW_S905L2_MT7668_线刷固件包
机器学习(十六):主成成分分析(PCA)
跨链桥已成行业最大安全隐患 为什么和怎么办
Minecraft HMCL 使用认证服务器LittleSkin进行登录
xgboost模块param中的一些错误
【LeetCode每日一题】——540.有序数组中的单一元素
Go语言gin框架返回json格式里,怎么把某个int属性转成string返回?
pygame的freetype模块
小满nestjs(第一章 介绍nestjs)
刷爆朋友圈!Alibaba出品亿级并发设计速成笔记太香了!
RTL8762DK 远端设备配对
服装店如何利用好积分?
Mobile magic box CM211-1_YS foundry _S905L3B_RTL8822C_wire brush firmware package
开一个羽毛球馆大概需要多少钱?大约15万左右可以搞定!
聚合收款码有限制吗?怎么办理?
SAP ABAP SteammPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克
学习探索-给字体设置前景色
智慧场馆的功能有哪些
理财产品买入后份额是固定不变的吗?
Heilongjiang Mobile New Magic Hundred Box M411A_2+8_S905L3A_wire brush firmware package









