8000 :sparkles: Added solution to 1128 · ninefive/awesome-golang-leetcode@f2b7626 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2b7626

Browse files
author
Sathish Babu
committed
✨ Added solution to 1128
1 parent 9fd63c9 commit f2b7626

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/1128.Number-of-Equivalent-Domino-Pairs/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
66
## Description
77

8+
Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rotated to be equal to another domino.
9+
10+
Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
11+
812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
16+
Output: 1
1317
```
1418

1519
## 题意
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(dominoes [][]int) int {
4+
n := len(dominoes)
5+
if n == 1 {
6+
return 0
7+
}
8+
hash, pairs := make(map[int]int), 0
9+
for _, val := range dominoes {
10+
newVal := changeVal(val)
11+
pairs += hash[newVal]
12+
hash[newVal]++
13+
}
14+
return pairs
15+
}
16+
17+
func changeVal(val []int) int {
18+
if val[0] > val[1] {
19+
val[0], val[1] = val[1], val[0]
20+
}
21+
return val[0]*10 + val[1]
522
}

src/1128.Number-of-Equivalent-Domino-Pairs/Solution_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", [][]int{{1, 2}, {2, 1}, {3, 4}, {5, 6}, {2, 1}, {5, 6}, {6, 5}}, 6},
17+
{"TestCase", [][]int{{1, 2}}, 0},
1918
}
2019

2120
// 开始测试

0 commit comments

Comments
 (0)
0