Skip to content

Commit 705cdb0

Browse files
committed
Add solution and test-cases for problem 3392
1 parent a01e89c commit 705cdb0

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [3392.Count Subarrays of Length Three With a Condition][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer array `nums`, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
Input: nums = [1,2,1,4,1]
10+
11+
Output: 1
12+
13+
Explanation:
1414
15-
## 题意
16-
> ...
15+
Only the subarray [1,4,1] contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.
16+
```
1717

18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Count Subarrays of Length Three With a Condition
23-
```go
2420
```
21+
Input: nums = [1,1,1]
22+
23+
Output: 0
2524
25+
Explanation:
26+
27+
[1,1,1] is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.
28+
```
2629

2730
## 结语
2831

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
ans := 0
5+
for i := 0; i < len(nums)-2; i++ {
6+
a := nums[i]
7+
b := nums[i+1]
8+
c := nums[i+2]
9+
if (a+c)*2 == b {
10+
ans++
11+
}
12+
}
13+
return ans
514
}

leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 1, 4, 1}, 1},
17+
{"TestCase2", []int{1, 1, 1}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)