914. X of a Kind in a Deck of Cards*

914. X of a Kind in a Deck of Cards*

https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/

题目描述

In a deck of cards, each card has an integer written on it.

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:

Each group has exactly X cards.
All the cards in each group have the same integer.

Example 1:

Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]

Example 2:

Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.

Example 3:

Input: [1]
Output: false
Explanation: No possible partition.

Example 4:

Input: [1,1]
Output: true
Explanation: Possible partition [1,1]

Example 5:

Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2]

Note:

  • 1 <= deck.length <= 10000
  • 0 <= deck[i] < 10000

C++ 实现 1

统计每个元素出现的次数, 之后本质上是求这些次数的最大公约数. (Greatest Common Divisor, GCD).
但我这里是先统计, 然后找其中最小的次数 min_val, 再判断 2 ~ min_val 中有没有能被所有次数整除的值.

class Solution {
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        if (deck.size() < 2) return false;
        unordered_map<int, int> records;
        int min_val = INT32_MAX;
        // 统计次数
        for (auto &a : deck) records[a] ++;
        for (auto &p : records) min_val = std::min(min_val, p.second);
        if (min_val < 2) return false;
        for (int i = 2; i <= min_val; ++ i) {
            int count = 0;
            for (auto &p : records) {
                if (p.second % i != 0) break; // 不能整除就直接 break
                count ++;
            }
            if (count == records.size()) return true; // 说明都能被整除
        }
        return false;
    }
};

C++ 实现 2

C++ 中的 <algorithm> 库提供了 __gcd(a, b) 函数库来求最大公约数 (Greatest Common Divisor, GCD). 代码来自 LeetCode Submission. 关于 __gcd 的介绍可以看:

class Solution {
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        unordered_map<int, int> count;
        int res = 0;
        for (int i : deck) count[i]++;
        for (auto i : count) res = __gcd(i.second, res);
        return res > 1;
    }
};
import random random.seed(10) class Deck: def init(self): ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] suits = ['C', 'D', 'H', 'S'] self.deck = [s + r for s in suits for r in ranks] random.shuffle(self.deck) def hand(self, n=1): #拿一手牌,n张 hand = [self.deck[i] for i in range(n)] del self.deck[:n] return hand def deal(self, cards_per_hand, no_of_players): #发牌,cards_per_hand:每个人发牌数, no_of_players:人数 return [self.hand(cards_per_hand) for i in range(no_of_players)] def str(self): return str(self.deck) def same_rank(hand, n_of_a_kind): #分析牌有对或者3-4个 #hand:一手牌,n_of_a_kind:2~4 寻找相同牌面有这么多的牌 ranks = [card[1:] for card in hand] counter = 0 already_counted = [] for rank in ranks: if rank not in already_counted and \ ranks.count(rank) == n_of_a_kind: counter += 1 already_counted.append(rank) return counter def same_suit(hand): #统计同花牌数量 suits = [card[0] for card in hand] counter = {} # counter[suit]存放suit花色的数量 for suit in suits: count = suits.count(suit) if count > 1: #只记录花色数大于1的 counter[suit] = count return counter N=100000 #实验次数 #计算五张牌中正好两对的概率 def prob_two_pairs(): #----------begin----------- #----------end----------- #五张牌中有四张或五张同一花色的牌的概率 def prob_same_suit(): #----------begin----------- #----------end----------- #计算五张牌中有四张牌面数字相同的牌(四条)的概率 def prob_fourofakind(): #----------begin----------- #----------end----------- print('五张牌中正好两对的概率:%.5f' %prob_two_pairs()) print('五张牌中有四张或五张同一花色的牌的概率:%.5f' %prob_same_suit()) print('五张牌中有四张牌面数字相同的牌(四条)的概率:%.5f' %prob_fourofakind())请补全代码
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值