前缀和代码

This commit is contained in:
3119005212
2021-04-27 15:11:05 +08:00
parent e8255d73ec
commit 8ef63207ac
5 changed files with 170 additions and 2 deletions

View File

@@ -43,6 +43,8 @@
我们来解析一下哈希表key 代表的是含有 1 个奇数的前缀区间value 代表这种子区间的个数含有两个也就是nums[0],nums[0,1].后面含义相同那我们下面直接看代码吧一下就能读懂
Java Code:
```java
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
@@ -70,8 +72,40 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
if (nums.size() == 0) {
return 0;
}
map <int, int> m;
//统计奇数个数,相当于我们的 presum
int oddnum = 0;
int count = 0;
m.insert({0,1});
for (int & x : nums) {
// 统计奇数个数
oddnum += x & 1;
// 发现存在,则 count增加
if (m.find(oddnum - k) != m.end()) {
count += m[oddnum - k];
}
//存入
if(m.find(oddnum) != m.end()) m[oddnum]++;
else m[oddnum] = 1;
}
return count;
}
};
```
但是也有一点不同就是我们是统计奇数的个数数组中的奇数个数肯定不会超过原数组的长度所以这个题目中我们可以用数组来模拟 HashMap 用数组的索引来模拟 HashMap key用值来模拟哈希表的 value下面我们直接看代码吧
Java Code:
```java
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
@@ -93,4 +127,27 @@ class Solution {
}
```
###
C++ Code:
```cpp
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
int len = nums.size();
vector <int> map(len + 1, 0);
map[0] = 1;
int oddnum = 0;
int count = 0;
for (int i = 0; i < len; ++i) {
//如果是奇数则加一偶数加0相当于没加
oddnum += nums[i] & 1;
if (oddnum - k >= 0) {
count += map[oddnum-k];
}
map[oddnum]++;
}
return count;
}
};
```