Skip to content

Commit 0f11f1d

Browse files
committed
Add solution and test-cases for problem 2176
1 parent a01e89c commit 0f11f1d

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# [2176.Count Equal and Divisible Pairs in an Array][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 a **0-indexed** integer array `nums` of length n and an integer `k`, return the **number of pairs** `(i, j)` where `0 <= i < j < n`, such that `nums[i] == nums[j]` and `(i * j)` is divisible by `k`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums = [3,1,2,2,2,1,3], k = 2
10+
Output: 4
11+
Explanation:
12+
There are 4 pairs that meet all the requirements:
13+
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
14+
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
15+
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
16+
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
1317
```
1418

15-
## 题意
16-
> ...
17-
18-
## 题解
19+
**Example 2:**
1920

20-
### 思路1
21-
> ...
22-
Count Equal and Divisible Pairs in an Array
23-
```go
2421
```
25-
22+
Input: nums = [1,2,3,4], k = 1
23+
Output: 0
24+
Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
25+
```
2626

2727
## 结语
2828

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
count := make(map[int][]int)
5+
for i, n := range nums {
6+
count[n] = append(count[n], i)
7+
}
8+
ans := 0
9+
for _, list := range count {
10+
l := len(list)
11+
for i := 0; i < l-1; i++ {
12+
for j := i + 1; j < l; j++ {
13+
if (list[i]*list[j])%k == 0 {
14+
ans++
15+
}
16+
}
17+
}
18+
}
19+
return ans
520
}

leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/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+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{3, 1, 2, 2, 2, 1, 3}, 2, 4},
18+
{"TestCase2", []int{1, 2, 3, 4}, 1, 0},
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.nums, c.k)
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.nums, c.k)
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)