blog/source/_posts/2020-03-29-leetcode-914.md
Ching 9690121403 feat(init project): add all existing files
add all existing files

Signed-off-by: Ching <loooching@gmail.com>
2022-02-02 19:04:18 +08:00

52 lines
909 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: leetcode-914
date: 2020-03-29 22:41:09
tags:
categories: leetcode
---
### 914. 卡牌分组
[题目](https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/)
<!--more-->
将大牌堆分成多个牌数量相等的小牌堆就是求每张牌数量的公约数。先遍历一遍得到每张牌的数量然后找出比2大的公约数即可。
```python
class Solution:
def hasGroupsSizeX(self, deck) -> bool:
dc = {}
max_d = 0
for d in deck:
if d not in dc:
dc[d] = 0
dc[d] += 1
if max_d < d:
max_d = d
if max_d < dc[d]:
max_d = dc[d]
has_x = True
if max_d == 1:
max_d = 2
for i in range(2, max_d + 1):
has_x = True
for k,v in dc.items():
if v % i:
has_x = False
break
if has_x and i >= 2:
return True
return False
#56 ms 13.8 MB
```