跳到主要内容

2023-8-29

8-29

数组

题号 剑指 Offer 61. 扑克牌中的顺子

若干副扑克牌中随机抽 5 张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。

题解https://leetcode.cn/problems/bu-ke-pai-zhong-de-shun-zi-lcof/solutions/212071/mian-shi-ti-61-bu-ke-pai-zhong-de-shun-zi-ji-he-se/

function isStraight(nums: number[]): boolean {
let repeat = new Set()
let max = 0,min = 14
for(let num of nums){
if(num===0)continue
max = Math.max(max,num)
min = Math.min(min,num)
if(repeat.has(num))return false
repeat.add(num)
}
return max -min<5
};