**输入:** 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" 手牌类型。
classSolution: defbestHand(self, ranks: List[int], suits: List[str]) -> str: iflen(set(suits)) == 1: return"Flush" h = Counter(ranks) iflen(h) == 5: return"High Card" for [a, b] in h.items(): if b > 2: return"Three of a Kind" return"Pair"
var bestHand = function(ranks, suits) { const suitsSet = newSet(); for (const suit of suits) { suitsSet.add(suit); } if (suitsSet.size === 1) { return"Flush"; } const h = newMap(); for (const rank of ranks) { h.set(rank, (h.get(rank) || 0) + 1); } if (h.size === 5) { return"High Card"; } for (const value of h.values()) { if (value > 2) { return"Three of a Kind"; } } return"Pair"; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcbestHand(ranks []int, suits []byte)string { if bytes.Count(suits, suits[:1]) == 5 { return"Flush" } cnt, pair := map[int]int{}, false for _, r := range ranks { cnt[r]++ if cnt[r] == 3 { return"Three of a Kind" } if cnt[r] == 2 { pair = true } } if pair { return"Pair" } return"High Card" }