Skip to content

Commit

Permalink
Merge pull request #550 from 0xff-dev/1512
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1512
  • Loading branch information
6boris authored Aug 3, 2023
2 parents 0a7f04f + 70eb86a commit a35b885
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
30 changes: 17 additions & 13 deletions leetcode/1501-1600/1512.Number-of-Good-Pairs/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
15 changes: 13 additions & 2 deletions leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 7 additions & 7 deletions leetcode/1501-1600/1512.Number-of-Good-Pairs/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit a35b885

Please sign in to comment.