Skip to content

Commit 847cd9b

Browse files
committed
Remove unused C++ file Luogu_P_1075.cpp, which contained basic input structure without implemented logic.
1 parent 007e4b8 commit 847cd9b

15 files changed

Lines changed: 725 additions & 18 deletions

File tree

Luogu_P_1075.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int subtractProductAndSum(int n) {
4+
long long digit_sum = 0;
5+
long long digit_multiply = 1;
6+
int flag = 0;
7+
while(n > 0){
8+
int digit = n % 10;
9+
if(digit == 0){
10+
flag = 1;
11+
}
12+
digit_sum += digit;
13+
digit_multiply *= digit;
14+
n /= 10;
15+
}
16+
if(flag == 1){
17+
return -digit_sum;
18+
}
19+
return digit_multiply - digit_sum;
20+
}
21+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def subtractProductAndSum(self, n: int) -> int:
3+
prod, s = 1,0
4+
while n > 0:
5+
prod *= n % 10
6+
s += n % 10
7+
8+
n //= 10
9+
10+
return prod - s
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
int countPartitions(vector<int>& nums) {
4+
int sum = reduce(nums.begin(),nums.end());
5+
return sum % 2 ? 0 : nums.size() - 1;
6+
}
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
int countPartitions(vector<int>& nums) {
4+
int sum = accumulate(nums.begin(),nums.end(),0);
5+
return sum % 2 ? 0 : nums.size() - 1;
6+
}
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public int countPartitions(int[] nums) {
3+
int sum = Arrays.stream(nums).sum();
4+
return sum % 2 != 0 ? 0 : nums.length - 1;
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def countPartitions(self, nums: List[int]) -> int:
3+
return 0 if sum(nums) % 2 else len(nums) - 1

leetcode/Study Plan/3432. 统计元素和差值为偶数的分区方案/2.cpp

Whitespace-only changes.
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
# 3432. 统计元素和差值为偶数的分区方案 - 代码解释
2+
3+
## 整体代码逻辑
4+
5+
```cpp
6+
class Solution {
7+
public:
8+
int countPartitions(vector<int>& nums) {
9+
int sum = accumulate(nums.begin(),nums.end(),0);
10+
return sum % 2 ? 0 : nums.size() - 1;
11+
}
12+
};
13+
```
14+
15+
### 代码功能
16+
这段代码用于统计将数组分成两部分,使得两部分元素和的差值为偶数的分区方案数。
17+
18+
### 执行流程
19+
1. **第4行**:计算数组所有元素的和
20+
2. **第5行**:根据总和的奇偶性返回结果
21+
- 如果总和是奇数 → 返回 0(无法找到差值为偶数的分区)
22+
- 如果总和是偶数 → 返回 `nums.size() - 1`(有 n-1 个分割位置)
23+
24+
---
25+
26+
## `accumulate` 函数详解
27+
28+
### 基本概念
29+
30+
`accumulate` 是 C++ `<numeric>` 头文件中的函数,用于**累加计算**。它可以将一个范围内的所有元素按照指定操作进行累积。
31+
32+
### 函数原型
33+
34+
```cpp
35+
template<class InputIt, class T>
36+
T accumulate(InputIt first, InputIt last, T init);
37+
```
38+
39+
### 参数说明
40+
41+
1. **`first`**:起始迭代器,指向要累加的第一个元素
42+
2. **`last`**:结束迭代器,指向要累加的最后一个元素的下一个位置(不包含)
43+
3. **`init`**:初始值,累加的起始值
44+
45+
### 工作原理
46+
47+
`accumulate` 函数会:
48+
1.`init` 开始
49+
2. 依次遍历 `[first, last)` 范围内的每个元素
50+
3. 将每个元素加到累加结果上
51+
4. 返回最终的累加结果
52+
53+
**等价于**
54+
```cpp
55+
T result = init;
56+
for (auto it = first; it != last; ++it) {
57+
result = result + *it; // 或者 result += *it;
58+
}
59+
return result;
60+
```
61+
62+
---
63+
64+
## 使用示例
65+
66+
### 示例1:基本用法 - 计算数组和
67+
68+
```cpp
69+
#include <iostream>
70+
#include <vector>
71+
#include <numeric> // 需要包含这个头文件
72+
73+
int main() {
74+
vector<int> nums = {1, 2, 3, 4, 5};
75+
76+
// 计算数组所有元素的和
77+
int sum = accumulate(nums.begin(), nums.end(), 0);
78+
// 等价于:0 + 1 + 2 + 3 + 4 + 5 = 15
79+
80+
cout << "数组和: " << sum << endl; // 输出: 15
81+
82+
return 0;
83+
}
84+
```
85+
86+
**执行过程**
87+
```
88+
初始值: 0
89+
0 + 1 = 1
90+
1 + 2 = 3
91+
3 + 3 = 6
92+
6 + 4 = 10
93+
10 + 5 = 15
94+
结果: 15
95+
```
96+
97+
### 示例2:从非零初始值开始
98+
99+
```cpp
100+
vector<int> nums = {10, 20, 30};
101+
102+
// 从100开始累加
103+
int result = accumulate(nums.begin(), nums.end(), 100);
104+
// 等价于:100 + 10 + 20 + 30 = 160
105+
106+
cout << result << endl; // 输出: 160
107+
```
108+
109+
### 示例3:计算乘积
110+
111+
```cpp
112+
vector<int> nums = {2, 3, 4};
113+
114+
// 计算乘积,初始值为1
115+
int product = accumulate(nums.begin(), nums.end(), 1,
116+
[](int a, int b) { return a * b; });
117+
// 等价于:1 * 2 * 3 * 4 = 24
118+
119+
cout << product << endl; // 输出: 24
120+
```
121+
122+
### 示例4:计算字符串连接
123+
124+
```cpp
125+
vector<string> words = {"Hello", " ", "World"};
126+
127+
string result = accumulate(words.begin(), words.end(), string(""));
128+
// 等价于:"" + "Hello" + " " + "World" = "Hello World"
129+
130+
cout << result << endl; // 输出: Hello World
131+
```
132+
133+
### 示例5:计算浮点数数组和
134+
135+
```cpp
136+
vector<double> nums = {1.5, 2.5, 3.5};
137+
138+
double sum = accumulate(nums.begin(), nums.end(), 0.0);
139+
// 注意:初始值要用 0.0 而不是 0,否则会丢失小数部分
140+
141+
cout << sum << endl; // 输出: 7.5
142+
```
143+
144+
---
145+
146+
## 代码中的使用
147+
148+
### 原代码分析
149+
150+
```cpp
151+
int sum = accumulate(nums.begin(), nums.end(), 0);
152+
```
153+
154+
**含义**
155+
- `nums.begin()`:指向数组第一个元素
156+
- `nums.end()`:指向数组最后一个元素的下一个位置
157+
- `0`:累加的初始值
158+
159+
**等价的手动实现**
160+
```cpp
161+
int sum = 0;
162+
for (int i = 0; i < nums.size(); i++) {
163+
sum += nums[i];
164+
}
165+
```
166+
167+
或者:
168+
```cpp
169+
int sum = 0;
170+
for (auto num : nums) {
171+
sum += num;
172+
}
173+
```
174+
175+
---
176+
177+
## 完整示例:理解整个代码
178+
179+
```cpp
180+
#include <iostream>
181+
#include <vector>
182+
#include <numeric>
183+
using namespace std;
184+
185+
class Solution {
186+
public:
187+
int countPartitions(vector<int>& nums) {
188+
// 计算数组所有元素的和
189+
int sum = accumulate(nums.begin(), nums.end(), 0);
190+
191+
// 如果总和是奇数,返回0;否则返回 n-1
192+
return sum % 2 ? 0 : nums.size() - 1;
193+
}
194+
};
195+
196+
int main() {
197+
Solution sol;
198+
199+
// 示例1:总和为偶数
200+
vector<int> nums1 = {1, 2, 3, 4}; // 总和 = 10(偶数)
201+
cout << "数组: [1,2,3,4]" << endl;
202+
cout << "总和: " << accumulate(nums1.begin(), nums1.end(), 0) << endl;
203+
cout << "分区方案数: " << sol.countPartitions(nums1) << endl;
204+
// 输出: 3(有3个分割位置:1|2,3,4 或 1,2|3,4 或 1,2,3|4)
205+
206+
cout << endl;
207+
208+
// 示例2:总和为奇数
209+
vector<int> nums2 = {1, 2, 3}; // 总和 = 6(偶数)
210+
cout << "数组: [1,2,3]" << endl;
211+
cout << "总和: " << accumulate(nums2.begin(), nums2.end(), 0) << endl;
212+
cout << "分区方案数: " << sol.countPartitions(nums2) << endl;
213+
// 输出: 2(有2个分割位置)
214+
215+
cout << endl;
216+
217+
// 示例3:总和为奇数
218+
vector<int> nums3 = {1, 2, 4}; // 总和 = 7(奇数)
219+
cout << "数组: [1,2,4]" << endl;
220+
cout << "总和: " << accumulate(nums3.begin(), nums3.end(), 0) << endl;
221+
cout << "分区方案数: " << sol.countPartitions(nums3) << endl;
222+
// 输出: 0(无法找到差值为偶数的分区)
223+
224+
return 0;
225+
}
226+
```
227+
228+
**运行结果**:
229+
```
230+
数组: [1,2,3,4]
231+
总和: 10
232+
分区方案数: 3
233+
234+
数组: [1,2,3]
235+
总和: 6
236+
分区方案数: 2
237+
238+
数组: [1,2,4]
239+
总和: 7
240+
分区方案数: 0
241+
```
242+
243+
---
244+
245+
## 注意事项
246+
247+
### 1. 需要包含头文件
248+
```cpp
249+
#include <numeric> // accumulate 函数需要这个头文件
250+
```
251+
252+
### 2. 初始值类型很重要
253+
```cpp
254+
// 对于整数数组,用 0
255+
int sum = accumulate(nums.begin(), nums.end(), 0);
256+
257+
// 对于浮点数数组,用 0.0
258+
double sum = accumulate(nums.begin(), nums.end(), 0.0);
259+
```
260+
261+
### 3. 迭代器范围
262+
- `[begin, end)`**左闭右开**区间
263+
- `begin` 指向第一个元素
264+
- `end` 指向最后一个元素的下一个位置(不包含)
265+
266+
### 4. 性能
267+
- `accumulate` 的时间复杂度是 O(n),n 是元素个数
268+
- 比手动循环更简洁,但性能基本相同
269+
270+
---
271+
272+
## 总结
273+
274+
1. **`accumulate` 函数**:用于累加计算,可以计算数组和、乘积等
275+
2. **基本用法**`accumulate(开始位置, 结束位置, 初始值)`
276+
3. **代码中的使用**:计算数组所有元素的和
277+
4. **优势**:代码简洁,可读性好,避免手动循环
278+
279+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def countPartitions(self, nums: List[int], k: int) -> int:
3+
MOD = 1_000_000_007
4+
n = len(nums)
5+
min_q = deque()
6+
max_q = deque()
7+
f = [0]*(n+1)
8+
f[0] = 1
9+
sum_f = 0
10+
left = 0
11+
12+
for i,x in enumerate(nums):
13+
sum_f += f[i]
14+
15+
while min_q and nums[min_q[-1]] >= x:
16+
min_q.pop()
17+
min_q.append(i)
18+
19+
while max_q and nums[max_q[-1]] <= x:
20+
max_q.pop()
21+
max_q.append(i)
22+
23+
while nums[max_q[0]] - nums[min_q[0]] > k:
24+
sum_f -= f[left]
25+
left += 1
26+
if min_q[0] < left:
27+
min_q.popleft()
28+
if max_q[0] < left:
29+
max_q.popleft()
30+
31+
f[i+1] = sum_f % MOD
32+
33+
return f[n]

0 commit comments

Comments
 (0)