diff --git a/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/README.md b/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/README.md index 915bbc7cf..4b77affcd 100755 --- a/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/README.md +++ b/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/README.md @@ -1,28 +1,28 @@ # [2176.Count Equal and Divisible Pairs in an Array][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 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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [3,1,2,2,2,1,3], k = 2 +Output: 4 +Explanation: +There are 4 pairs that meet all the requirements: +- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2. +- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2. +- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2. +- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Count Equal and Divisible Pairs in an Array -```go ``` - +Input: nums = [1,2,3,4], k = 1 +Output: 0 +Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements. +``` ## 结语 diff --git a/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution.go b/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution.go index d115ccf5e..518d35ad0 100644 --- a/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution.go +++ b/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + count := make(map[int][]int) + for i, n := range nums { + count[n] = append(count[n], i) + } + ans := 0 + for _, list := range count { + l := len(list) + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + if (list[i]*list[j])%k == 0 { + ans++ + } + } + } + } + return ans } diff --git a/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution_test.go b/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution_test.go index 14ff50eb4..1d2a0f81e 100644 --- a/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution_test.go +++ b/leetcode/2101-2200/2176.Count-Equal-and-Divisible-Pairs-in-an-Array/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 1, 2, 2, 2, 1, 3}, 2, 4}, + {"TestCase2", []int{1, 2, 3, 4}, 1, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }