Skip to content

Commit 5c0f7ca

Browse files
authored
Merge pull request chefyuan#19 from coderhare/main
前缀和专题补充代码
2 parents 65bc79b + 376bec8 commit 5c0f7ca

5 files changed

+170
-2
lines changed

animation-simulation/前缀和/leetcode1248寻找优美子数组.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
我们来解析一下哈希表,key 代表的是含有 1 个奇数的前缀区间,value 代表这种子区间的个数,含有两个,也就是nums[0],nums[0,1].后面含义相同,那我们下面直接看代码吧,一下就能读懂。
4545

46+
Java Code:
47+
4648
```java
4749
class Solution {
4850
public int numberOfSubarrays(int[] nums, int k) {
@@ -70,8 +72,40 @@ class Solution {
7072
}
7173
```
7274

75+
C++ Code:
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int numberOfSubarrays(vector<int>& nums, int k) {
81+
if (nums.size() == 0) {
82+
return 0;
83+
}
84+
map <int, int> m;
85+
//统计奇数个数,相当于我们的 presum
86+
int oddnum = 0;
87+
int count = 0;
88+
m.insert({0,1});
89+
for (int & x : nums) {
90+
// 统计奇数个数
91+
oddnum += x & 1;
92+
// 发现存在,则 count增加
93+
if (m.find(oddnum - k) != m.end()) {
94+
count += m[oddnum - k];
95+
}
96+
//存入
97+
if(m.find(oddnum) != m.end()) m[oddnum]++;
98+
else m[oddnum] = 1;
99+
}
100+
return count;
101+
}
102+
};
103+
```
104+
73105
但是也有一点不同,就是我们是统计奇数的个数,数组中的奇数个数肯定不会超过原数组的长度,所以这个题目中我们可以用数组来模拟 HashMap ,用数组的索引来模拟 HashMap 的 key,用值来模拟哈希表的 value。下面我们直接看代码吧。
74106
107+
Java Code:
108+
75109
```java
76110
class Solution {
77111
public int numberOfSubarrays(int[] nums, int k) {
@@ -93,4 +127,27 @@ class Solution {
93127
}
94128
```
95129

96-
###
130+
C++ Code:
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
int numberOfSubarrays(vector<int>& nums, int k) {
136+
int len = nums.size();
137+
vector <int> map(len + 1, 0);
138+
map[0] = 1;
139+
int oddnum = 0;
140+
int count = 0;
141+
for (int i = 0; i < len; ++i) {
142+
//如果是奇数则加一,偶数加0,相当于没加
143+
oddnum += nums[i] & 1;
144+
if (oddnum - k >= 0) {
145+
count += map[oddnum-k];
146+
}
147+
map[oddnum]++;
148+
}
149+
return count;
150+
}
151+
};
152+
```
153+

animation-simulation/前缀和/leetcode523连续的子数组和.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
**题目代码**
4848

49+
Java Code:
50+
4951
```java
5052
class Solution {
5153
public boolean checkSubarraySum(int[] nums, int k) {
@@ -71,5 +73,31 @@ class Solution {
7173
}
7274
```
7375

76+
C++ Code:
7477

78+
```cpp
79+
class Solution {
80+
public:
81+
bool checkSubarraySum(vector<int>& nums, int k) {
82+
map <int, int> m;
83+
//细节2
84+
m.insert({0,-1});
85+
int presum = 0;
86+
for (int i = 0; i < nums.size(); ++i) {
87+
presum += nums[i];
88+
//细节1,防止 k 为 0 的情况
89+
int key = k == 0 ? presum : presum % k;
90+
if (m.find(key) != m.end()) {
91+
if (i - m[key] >= 2) {
92+
return true;
93+
}
94+
//因为我们需要保存最小索引,当已经存在时则不用再次存入,不然会更新索引值
95+
continue;
96+
}
97+
m.insert({key, i});
98+
}
99+
return false;
100+
}
101+
};
102+
```
75103

animation-simulation/前缀和/leetcode560和为K的子数组.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Solution {
122122

123123
**题目代码**
124124

125+
Java Code:
126+
125127
```java
126128
class Solution {
127129
public int subarraySum(int[] nums, int k) {
@@ -150,3 +152,34 @@ class Solution {
150152
}
151153
```
152154

155+
C++ Code:
156+
157+
```cpp
158+
public:
159+
int subarraySum(vector<int>& nums, int k) {
160+
if (nums.size() == 0) {
161+
return 0;
162+
}
163+
map <int, int> m;
164+
//细节,这里需要预存前缀和为 0 的情况,会漏掉前几位就满足的情况
165+
//例如输入[1,1,0],k = 2 如果没有这行代码,则会返回0,漏掉了1+1=2,和1+1+0=2的情况
166+
//输入:[3,1,1,0] k = 2时则不会漏掉
167+
//因为presum[3] - presum[0]表示前面 3 位的和,所以需要m.insert({0,1}),垫下底
168+
m.insert({0, 1});
169+
int count = 0;
170+
int presum = 0;
171+
for (int x : nums) {
172+
presum += x;
173+
//当前前缀和已知,判断是否含有 presum - k的前缀和,那么我们就知道某一区间的和为 k 了。
174+
if (m.find(presum - k) != m.end()) {
175+
count += m[presum - k];//获取presum-k前缀和出现次数
176+
}
177+
//更新
178+
if(m.find(presum) != m.end()) m[presum]++;
179+
else m[presum] = 1;
180+
}
181+
return count;
182+
}
183+
};
184+
```
185+

animation-simulation/前缀和/leetcode724寻找数组的中心索引.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

6262
理解了我们前缀和的概念(不知道好像也可以做,这个题太简单了哈哈)。我们可以一下就能把这个题目做出来,先遍历一遍求出数组的和,然后第二次遍历时,直接进行对比左半部分和右半部分是否相同,如果相同则返回 true,不同则继续遍历。
6363

64+
Java Code:
65+
6466
```java
6567
class Solution {
6668
public int pivotIndex(int[] nums) {
@@ -82,4 +84,27 @@ class Solution {
8284
}
8385
```
8486

85-
###
87+
C++ Code:
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int pivotIndex(vector<int>& nums) {
93+
int presum = 0;
94+
//数组的和
95+
for (int x : nums) {
96+
presum += x;
97+
}
98+
int leftsum = 0;
99+
for (int i = 0; i < nums.size(); ++i) {
100+
//发现相同情况
101+
if (leftsum == presum - nums[i] - leftsum) {
102+
return i;
103+
}
104+
leftsum += nums[i];
105+
}
106+
return -1;
107+
}
108+
};
109+
```
110+

animation-simulation/前缀和/leetcode974和可被K整除的子数组.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ int key = (presum % K + K) % K;
8787

8888
那么这个题目我们可不可以用数组,代替 map 呢?当然也是可以的,因为此时我们的哈希表存的是余数,余数最大也只不过是 K-1所以我们可以用固定长度 K 的数组来模拟哈希表。
8989

90+
Java Code:
91+
9092
```java
9193
class Solution {
9294
public int subarraysDivByK(int[] A, int K) {
@@ -107,3 +109,26 @@ class Solution {
107109
}
108110
```
109111

112+
C++ Code:
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int subarraysDivByK(vector<int>& A, int K) {
118+
vector <int> map (K, 0);
119+
int len = A.size();
120+
int count = 0;
121+
int presum = 0;
122+
map[0] = 1;
123+
for (int i = 0; i < len; ++i) {
124+
presum += A[i];
125+
//求key
126+
int key = (presum % K + K) % K;
127+
//count添加次数,并将当前的map[key]++;
128+
count += (map[key]++);
129+
}
130+
return count;
131+
}
132+
};
133+
```
134+

0 commit comments

Comments
 (0)