LeetCode 题解工作台

最好的扑克手牌

给你一个整数数组 ranks 和一个字符数组 suit 。你有 5 张扑克牌,第 i 张牌大小为 ranks[i] ,花色为 suits[i] 。 下述是从好到坏你可能持有的 手牌类型 : "Flush" :同花,五张相同花色的扑克牌。 "Three of a Kind" :三条,有 3 张大小相同…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·哈希·扫描

bolt

答案摘要

我们可以先遍历数组 ,判断相邻两个元素是否均相等,如果是,则返回 `"Flush"`。 接下来,我们用哈希表或数组 统计每张牌的数量:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 ranks 和一个字符数组 suit 。你有 5 张扑克牌,第 i 张牌大小为 ranks[i] ,花色为 suits[i] 。

下述是从好到坏你可能持有的 手牌类型 

  1. "Flush":同花,五张相同花色的扑克牌。
  2. "Three of a Kind":三条,有 3 张大小相同的扑克牌。
  3. "Pair":对子,两张大小一样的扑克牌。
  4. "High Card":高牌,五张大小互不相同的扑克牌。

请你返回一个字符串,表示给定的 5 张牌中,你能组成的 最好手牌类型 。

注意:返回的字符串 大小写 需与题目描述相同。

 

示例 1:

输入:ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
输出:"Flush"
解释:5 张扑克牌的花色相同,所以返回 "Flush" 。

示例 2:

输入:ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
输出:"Three of a Kind"
解释:第一、二和四张牌组成三张相同大小的扑克牌,所以得到 "Three of a Kind" 。
注意我们也可以得到 "Pair" ,但是 "Three of a Kind" 是更好的手牌类型。
有其他的 3 张牌也可以组成 "Three of a Kind" 手牌类型。

示例 3:

输入:ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
输出:"Pair"
解释:第一和第二张牌大小相同,所以得到 "Pair" 。
我们无法得到 "Flush" 或者 "Three of a Kind" 。

 

提示:

  • ranks.length == suits.length == 5
  • 1 <= ranks[i] <= 13
  • 'a' <= suits[i] <= 'd'
  • 任意两张扑克牌不会同时有相同的大小和花色。
lightbulb

解题思路

方法一:计数

我们可以先遍历数组 suits\textit{suits},判断相邻两个元素是否均相等,如果是,则返回 "Flush"

接下来,我们用哈希表或数组 cnt\textit{cnt} 统计每张牌的数量:

  • 如果有任意一张牌的数量等于 33,返回 "Three of a Kind"
  • 否则,如果有任意一张牌的数量等于 22,返回 "Pair"
  • 否则,返回 "High Card"

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为数组 ranks\textit{ranks} 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def bestHand(self, ranks: List[int], suits: List[str]) -> str:
        # if len(set(suits)) == 1:
        if all(a == b for a, b in pairwise(suits)):
            return 'Flush'
        cnt = Counter(ranks)
        if any(v >= 3 for v in cnt.values()):
            return 'Three of a Kind'
        if any(v == 2 for v in cnt.values()):
            return 'Pair'
        return 'High Card'
speed

复杂度分析

指标
时间complexity is O(5) for scanning suits and O(5) for counting ranks, effectively constant due to fixed array size. Space complexity is O(1) as the rank hash map stores at most 13 entries.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for early exit on Flush condition before counting ranks.

  • question_mark

    Check rank frequencies carefully; interviewer may ask about ties between Pair and Three of a Kind.

  • question_mark

    Consider edge cases where multiple hands satisfy conditions and confirm strongest hand is selected.

warning

常见陷阱

外企场景
  • error

    Checking rank counts before verifying Flush can return incorrect lower hand.

  • error

    Ignoring that multiple cards with same rank can form different hand combinations.

  • error

    Overcomplicating with sorting when simple counting and array scan suffices.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Determine best hand for 7-card poker using the same array scanning plus hash lookup pattern.

  • arrow_right_alt

    Adjust to handle repeated ranks in non-unique card decks, updating hash counting logic.

  • arrow_right_alt

    Identify second-best hand if strongest hand is removed, requiring conditional checks in order.

help

常见问题

外企场景

最好的扑克手牌题解:数组·哈希·扫描 | LeetCode #2347 简单