8000 :sparkles: Added solution to 914 · reverse/awesome-golang-leetcode@aec3a37 · GitHub
[go: up one dir, main page]

Skip to content

Commit aec3a37

Browse files
author
Sathish Babu
committed
✨ Added solution to 914
1 parent 2dcab61 commit aec3a37

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

src/0914.X-of-a-Kind-in-a-Deck-of-Cards/README.md

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

8+
In a deck of cards, each card has an integer written on it.
9+
10+
Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:
11+
12+
Each group has exactly X cards.
13+
All the cards in each group have the same integer.
14+
15+
816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: deck = [1,2,3,4,4,3,2,1]
20+
Output: true
21+
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4].
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: deck = [1,1,1,2,2,2,3,3]
28+
Output: false´
29+
Explanation: No possible partition.
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: deck = [1]
36+
Output: false
37+
Explanation: No possible partition.
38+
```
39+
40+
**Example 4:**
41+
42+
```
43+
Input: deck = [1,1]
44+
Output: true
45+
Explanation: Possible partition [1,1].
46+
```
47+
48+
**Example 5:**
49+
50+
```
51+
Input: deck = [1,1,2,2,2,2]
52+
Output: true
53+
Explanation: Possible partition [1,1],[2,2],[2,2].
1354
```
1455

1556
## 题意
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
func Solution(deck []int) bool {
4+
n := len(deck)
5+
if n < 2 {
6+
return false
7+
}
8+
hash := make(map[int]int)
9+
for _, val := range deck {
10+
hash[val]++
11+
}
12+
ans := hash[deck[0]]
13+
for _, val := range hash {
14+
ans = gcd(ans, val)
15+
}
16+
return ans >= 2
17+
}
18+
19+
func gcd(x, y int) int {
20+
for y > 0 {
21+
x, y = y, x%y
22+
}
423
return x
524
}

src/0914.X-of-a-Kind-in-a-Deck-of-Cards/Solution_test.go

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

2123
// 开始测试

0 commit comments

Comments
 (0)
0