Skip to content

Commit 7884544

Browse files
committed
update: 添加问题“3392.统计符合条件长度为3的子数组数目”的代码和题解(#903)
Signed-off-by: LetMeFly666 <814114971@qq.com>
1 parent bb6a6ea commit 7884544

12 files changed

+418
-24
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-27 23:47:30
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-27 23:48:38
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countSubarrays(vector<int>& nums) {
14+
int ans = 0;
15+
for (int i = 2; i < nums.size(); i++) {
16+
ans += (nums[i] + nums[i - 2]) * 2 == nums[i - 1];
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-27 23:49:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-27 23:52:54
6+
* @Description: AC,100.00%,95.45%
7+
*/
8+
package main
9+
10+
func countSubarrays(nums []int) (ans int) {
11+
for i := 2; i < len(nums); i++ {
12+
if (nums[i] + nums[i - 2]) * 2 == nums[i - 1] {
13+
ans++
14+
}
15+
}
16+
return
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-27 23:49:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-27 23:50:25
6+
*/
7+
class Solution {
8+
public int countSubarrays(int[] nums) {
9+
int ans = 0;
10+
for (int i = 2; i < nums.length; i++) {
11+
if ((nums[i - 2] + nums[i]) * 2 == nums[i - 1]) {
12+
ans++;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-27 23:49:08
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-27 23:49:26
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countSubarrays(self, nums: List[int]) -> int:
11+
return sum((nums[i - 2] + nums[i]) * 2 == nums[i - 1] for i in range(2, len(nums)))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@
955955
|3306.元音辅音字符串计数II|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/13/LeetCode%203306.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146228751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/3609545/letmefly-3306yuan-yin-fu-yin-zi-fu-chuan-7y2q/" target="_blank">LeetCode题解</a>|
956956
|3340.检查平衡字符串|简单|<a href="https://leetcode.cn/problems/check-balanced-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/14/LeetCode%203340.%E6%A3%80%E6%9F%A5%E5%B9%B3%E8%A1%A1%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146249653" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-balanced-string/solutions/3610899/letmefly-3340jian-cha-ping-heng-zi-fu-ch-p8eo/" target="_blank">LeetCode题解</a>|
957957
|3375.使数组的值全部为K的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/09/LeetCode%203375.%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%80%BC%E5%85%A8%E9%83%A8%E4%B8%BAK%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147104288" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/solutions/3646138/letmefly-3375shi-shu-zu-de-zhi-quan-bu-w-i71e/" target="_blank">LeetCode题解</a>|
958+
|3392.统计符合条件长度为3的子数组数目|简单|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147603015" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/solutions/3665081/letmefly-3392tong-ji-fu-he-tiao-jian-cha-h935/" target="_blank">LeetCode题解</a>|
958959
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|
959960
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
960961
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: 3392.统计符合条件长度为 3 的子数组数目:一次遍历模拟
3+
date: 2025-04-29 10:24:45
4+
tags: [题解, LeetCode, 简单, 数组]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3392.统计符合条件长度为 3 的子数组数目:一次遍历模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/](https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/)
11+
12+
<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;,请你返回长度为 3 的 <span data-keyword="subarray-nonempty">子数组</span>,满足第一个数和第三个数的和恰好为第二个数的一半。</p>
13+
14+
<p><strong>子数组</strong>&nbsp;指的是一个数组中连续 <strong>非空</strong>&nbsp;的元素序列。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong class="example">示例 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><span class="example-io"><b>输入:</b>nums = [1,2,1,4,1]</span></p>
22+
23+
<p><span class="example-io"><b>输出:</b>1</span></p>
24+
25+
<p><b>解释:</b></p>
26+
27+
<p>只有子数组&nbsp;<code>[1,4,1]</code>&nbsp;包含 3 个元素且第一个和第三个数字之和是中间数字的一半。number.</p>
28+
</div>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><span class="example-io"><b>输入:</b>nums = [1,1,1]</span></p>
34+
35+
<p><span class="example-io"><b>输出:</b>0</span></p>
36+
37+
<p><b>解释:</b></p>
38+
39+
<p><code>[1,1,1]</code>&nbsp;是唯一长度为 3 的子数组,但第一个数和第三个数的和不是第二个数的一半。</p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
48+
<li><code><font face="monospace">-100 &lt;= nums[i] &lt;= 100</font></code></li>
49+
</ul>
50+
51+
52+
53+
## 解题方法:一次遍历模拟
54+
55+
用变量$i$从第三个元素开始向后遍历数组,若$(nums[i] + nums[i - 2]) * 2 == nums[i - 1]$,则答案数量加一。
56+
57+
+ 时间复杂度$O(len(nums))$
58+
+ 空间复杂度$O(1)$
59+
60+
### AC代码
61+
62+
#### C++
63+
64+
```cpp
65+
/*
66+
* @Author: LetMeFly
67+
* @Date: 2025-04-27 23:47:30
68+
* @LastEditors: LetMeFly.xyz
69+
* @LastEditTime: 2025-04-27 23:48:38
70+
*/
71+
class Solution {
72+
public:
73+
int countSubarrays(vector<int>& nums) {
74+
int ans = 0;
75+
for (int i = 2; i < nums.size(); i++) {
76+
ans += (nums[i] + nums[i - 2]) * 2 == nums[i - 1];
77+
}
78+
return ans;
79+
}
80+
};
81+
```
82+
83+
#### Python
84+
85+
```python
86+
'''
87+
Author: LetMeFly
88+
Date: 2025-04-27 23:49:08
89+
LastEditors: LetMeFly.xyz
90+
LastEditTime: 2025-04-27 23:49:26
91+
'''
92+
from typing import List
93+
94+
class Solution:
95+
def countSubarrays(self, nums: List[int]) -> int:
96+
return sum((nums[i - 2] + nums[i]) * 2 == nums[i - 1] for i in range(2, len(nums)))
97+
```
98+
99+
#### Golang
100+
101+
```go
102+
/*
103+
* @Author: LetMeFly
104+
* @Date: 2025-04-27 23:49:15
105+
* @LastEditors: LetMeFly.xyz
106+
* @LastEditTime: 2025-04-27 23:52:54
107+
* @Description: AC,100.00%,95.45%
108+
*/
109+
package main
110+
111+
func countSubarrays(nums []int) (ans int) {
112+
for i := 2; i < len(nums); i++ {
113+
if (nums[i] + nums[i - 2]) * 2 == nums[i - 1] {
114+
ans++
115+
}
116+
}
117+
return
118+
}
119+
```
120+
121+
#### Java
122+
123+
```java
124+
/*
125+
* @Author: LetMeFly
126+
* @Date: 2025-04-27 23:49:11
127+
* @LastEditors: LetMeFly.xyz
128+
* @LastEditTime: 2025-04-27 23:50:25
129+
*/
130+
class Solution {
131+
public int countSubarrays(int[] nums) {
132+
int ans = 0;
133+
for (int i = 2; i < nums.length; i++) {
134+
if ((nums[i - 2] + nums[i]) * 2 == nums[i - 1]) {
135+
ans++;
136+
}
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147603015)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/)哦~
144+
>
145+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ categories: [自用]
676676
|<font color="#28bea0" title="二次复习">perturbed</font>|adj. 焦虑的,不安的,烦躁的<br/>v. 使焦虑,使不安|
677677
|garrison|n. 卫戍部队,守备不对,卫戍区,驻防地<br/>v. 驻防,派(兵)驻守|
678678
|woodpecker|n. 啄木鸟|
679-
|<font color="#28bea0" title="二次复习">cosmetics</font>|n. 化妆品|
679+
|<font color="#28bea0" title="三次复习">cosmetics</font>|n. 化妆品|
680680
|violet|n. 紫罗兰,紫罗兰色,蓝紫色<br/>adj. 紫罗兰色的|
681681
|stow|v. 妥善放置,把…收好|
682682
|presently|adv. 一会儿,不久,现在,目前<br/>Tisfy: 当下这个意思与currently类似|
@@ -876,7 +876,7 @@ categories: [自用]
876876
|butchery|n. 残杀,杀戮,屠宰工作|
877877
|||
878878
|secretariat|n. 秘书处,书记处|
879-
|<font color="#28bea0" title="二次复习">sheriff</font>|n. 县(郡)治安官|
879+
|<font color="#28bea0" title="三次复习">sheriff</font>|n. 县(郡)治安官|
880880
|inasmuch|adv. 由于,〈古〉只要<details><summary>例句</summary>Inasmuch as you are an adult, you must be responsible for your own behavior.<br/>因为你是一个成年人,你必须对自己的行为负责。</details>|
881881
|countenance|n. 面容,脸色,面部表情<br/>v. 支持,赞成,同意|
882882
|||
@@ -1115,6 +1115,12 @@ categories: [自用]
11151115
|bottle-neck|n. 瓶颈,道路上的狭窄处|
11161116
|||
11171117
|perimeter|n. 周长,外缘,边缘|
1118+
|||
1119+
|luncheon|n. 午宴,正事的午餐|
1120+
|voiceless|ajd. (语音)发清音的,无法出声的,无发言权的|
1121+
|frying-pan|n. 煎锅,长柄平锅|
1122+
|||
1123+
|lining|n. 内衬,衬层,衬里|
11181124

11191125
<p class="wordCounts">单词收录总数</p>
11201126

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ xx型
241241
|誕生日(たんじょうび)|生日|
242242
|別れ(わかれ)|分手/告别|
243243
|デート|约会|
244+
|喧嘩(けんか)|吵架|
245+
|遅(おくれ)|迟到|
244246
|||
245247
|話せ(はなせ)|会说|
246248
|住んで(すんで)||
@@ -333,6 +335,7 @@ xx型
333335
|長い(ながい)|长的|
334336
|短い(みじかい)|短的|
335337
|いい|好的(不错的)|
338+
|最悪(さいあく)|最糟糕的|
336339
|古い(ふるい)|旧的|
337340
|近い(ちかい)|近的|
338341
|遠い(とおい)|远的|
@@ -344,6 +347,7 @@ xx型
344347
|可愛い(かわいい)|可爱的|
345348
|うるさい|吵的|
346349
|綺麗(きれい)|漂亮的/干净的|
350+
|(ダサく)|土气的/不酷的|
347351
|難しい(むずかしい)|难的|
348352
|静か(しずか)|安静的|
349353
|にぎやか|热闹的|
@@ -893,6 +897,7 @@ xx型
893897
|違う(ちがう)|不对/不同|
894898
|素敵(すてき)|好/漂亮|
895899
|必ず(かならず)|一定|
900+
|今回(こんかい)|这次|
896901
|||
897902
|牛肉(ぎゅうにく)|牛肉|
898903
|豚肉(ぶたにく)|猪肉|

tryGoPy/MGJW/temp/周报.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)