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 ce78566

Browse files
committed
Merge pull request #373 from 0xff-dev/2367
Add solution and test-cases for problem 2367
2 parents 281d855 + a8aa76f commit ce78566

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

‎leetcode/2301-2400/2367.Number-of-Arithmetic-Triplets/README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [2367.Number of Arithmetic Triplets][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+
You are given a **0-indexed, strictly increasing** integer array `nums` and a positive integer `diff`. A triplet (`i, j, k`) is an **arithmetic triplet** if the following conditions are met:
5+
6+
- `i < j < k`,
7+
- `nums[j] - nums[i] == diff`, and
8+
- `nums[k] - nums[j] == diff`.
9+
10+
Return the number of unique **arithmetic triplets**.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums = [0,1,4,6,7,10], diff = 3
16+
Output: 2
17+
Explanation:
18+
(1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
19+
(2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
1320
```
1421

15-
## 题意
16-
> ...
17-
18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of Arithmetic Triplets
23-
```go
22+
**Example 2:**
23+
```
24+
Input: nums = [4,5,6,7,8,9], diff = 2
25+
Output: 2
26+
Explanation:
27+
(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
28+
(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
2429
```
25-
2630

2731
## 结语
2832

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, diff int) int {
4+
count := 0
5+
6+
bucket := make([]int, 201)
7+
for i, n := range nums {
8+
bucket[n] = i + 1
9+
}
10+
11+
for idx := 0; idx <= len(nums)-3; idx++ {
12+
next := nums[idx] + diff
13+
if !(next <= 200 && bucket[next] > 0) {
14+
continue
15+
}
16+
nextNext := next + diff
17+
if !(nextNext <= 200 && bucket[nextNext] > 0) {
18+
continue
19+
}
20+
count++
21+
}
22+
return count
523
}

‎leetcode/2301-2400/2367.Number-of-Arithmetic-Triplets/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.diff)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.diff)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
(0)

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