Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8aff918

Browse files
✨Update: Modify 1588
1 parent 16d6f99 commit 8aff918

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

‎LeetCode/1581-1590/1588. 所有奇数长度子数组的和(简单).md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ class Solution {
8484

8585
### 数学
8686

87-
事实上,我们可以统计任意只 $arr[i]$ 在奇数子数组的出现次数。
87+
事实上,我们可以统计任意值 $arr[i]$ 在奇数子数组的出现次数。
8888

8989
对于原数组的任意位置 $i$ 而言,其左边共有 $i$ 个数,右边共有 $n - i - 1$ 个数。
9090

9191
**$arr[i]$ 作为某个奇数子数组的成员的充要条件为:其所在奇数子数组左右两边元素个数奇偶性相同。**
9292

93-
于是问题转换为如何求得「$arr[i]$ 在原数组中左边连续一段元素个为奇数的方案数」和「$arr[i]$ 在原数组右边连续一段元素个数为偶数的方案数」。
93+
于是问题转换为如何求得「$arr[i]$ 在原数组中两边连续一段元素个为奇数的方案数」和「$arr[i]$ 在原数组两边连续一段元素个数为偶数的方案数」。
9494

9595
由于我们已经知道 $arr[i]$ 左边共有 $i$ 个数,右边共有 $n - i - 1$ 个数,因此可以算得组合数:
9696

9797
* 位置 $i$ 左边奇数个数的方案数为 $(i + 1) / 2,ドル右边奇数个数的方案数为 $(n - i) / 2$;
98-
* 位置 $i$ 右边偶数(非零)个数的方案数为 $i / 2,ドル右边偶数(非零)个数的方案数为 $(n - i - 1) / 2$;
98+
* 位置 $i$ 左边偶数(非零)个数的方案数为 $i / 2,ドル右边偶数(非零)个数的方案数为 $(n - i - 1) / 2$;
9999
* 考虑左右两边不选也属于合法的偶数个数方案数,因此在上述分析基础上对偶数方案数自增 1ドル$。
100100

101101
至此,我们得到了位置 $i$ 左右奇数和偶数的方案数个数,根据「如果 $arr[i]$ 位于奇数子数组中,其左右两边元素个数奇偶性相同」以及「乘法原理」,我们知道 $arr[i]$ 同出现在多少个奇数子数组中,再乘上 $arr[i]$ 即是 $arr[i]$ 对答案的贡献。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /