Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaec3a37

Browse files
author
Sathish Babu
committed
✨ Added solution to 914
1 parent2dcab61 commitaec3a37

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-
funcSolution(xbool)bool {
3+
funcSolution(deck []int)bool {
4+
n:=len(deck)
5+
ifn<2 {
6+
returnfalse
7+
}
8+
hash:=make(map[int]int)
9+
for_,val:=rangedeck {
10+
hash[val]++
11+
}
12+
ans:=hash[deck[0]]
13+
for_,val:=rangehash {
14+
ans=gcd(ans,val)
15+
}
16+
returnans>=2
17+
}
18+
19+
funcgcd(x,yint)int {
20+
fory>0 {
21+
x,y=y,x%y
22+
}
423
returnx
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
namestring
13-
inputsbool
13+
inputs[]int
1414
expectbool
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)

[8]ページ先頭

©2009-2025 Movatter.jp