diff --git a/leetcode/1501-1600/1512.Number-of-Good-Pairs/README.md b/leetcode/1501-1600/1512.Number-of-Good-Pairs/README.md index 9ec3b2a0d..c837f9a7b 100755 --- a/leetcode/1501-1600/1512.Number-of-Good-Pairs/README.md +++ b/leetcode/1501-1600/1512.Number-of-Good-Pairs/README.md @@ -1,28 +1,32 @@ # [1512.Number of Good Pairs][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 array of integers `nums`, return the number of **good pairs**. + +A pair `(i, j)` is called good if `nums[i] == nums[j]` and `i < j`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,3,1,1,3] +Output: 4 +Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Number of Good Pairs -```go +``` +Input: nums = [1,1,1,1] +Output: 6 +Explanation: Each pair in the array are good. ``` +**Example 3:** + +``` +Input: nums = [1,2,3] +Output: 0 +``` ## 结语 diff --git a/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution.go b/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution.go index d115ccf5e..07e836226 100644 --- a/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution.go +++ b/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + count := make([]int, 101) + for _, item := range nums { + count[item]++ + } + ans := 0 + for i := 1; i <= 100; i++ { + if count[i] > 1 { + n := (count[i] - 1) * count[i] + ans += n / 2 + } + } + return ans } diff --git a/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution_test.go b/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution_test.go index 14ff50eb4..5b006e12e 100644 --- a/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution_test.go +++ b/leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution_test.go @@ -10,12 +10,12 @@ 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, 3, 1, 1, 3}, 4}, + {"TestCase2", []int{1, 1, 1, 1}, 6}, + {"TestCase3", []int{1, 2, 3}, 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }