diff --git a/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/README.md b/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/README.md index c4ee8731..e0e9f418 100755 --- a/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/README.md +++ b/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/README.md @@ -1,28 +1,31 @@ # [3392.Count Subarrays of Length Three With a Condition][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [1,2,1,4,1] + +Output: 1 + +Explanation: -## 题意 -> ... +Only the subarray [1,4,1] contains exactly 3 elements where the sum of the first and third numbers equals half the middle number. +``` -## 题解 +**Example 2:** -### 思路1 -> ... -Count Subarrays of Length Three With a Condition -```go ``` +Input: nums = [1,1,1] + +Output: 0 +Explanation: + +[1,1,1] is the only subarray of length 3. However, its first and third numbers do not add to half the middle number. +``` ## 结语 diff --git a/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution.go b/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution.go index d115ccf5..e641c95e 100644 --- a/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution.go +++ b/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution.go @@ -1,5 +1,14 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + ans := 0 + for i := 0; i < len(nums)-2; i++ { + a := nums[i] + b := nums[i+1] + c := nums[i+2] + if (a+c)*2 == b { + ans++ + } + } + return ans } diff --git a/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution_test.go b/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution_test.go index 14ff50eb..5274133e 100644 --- a/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution_test.go +++ b/leetcode/3301-3400/3392.Count-Subarrays-of-Length-Three-With-a-Condition/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 1, 4, 1}, 1}, + {"TestCase2", []int{1, 1, 1}, 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }