题目
- 从若干副扑克牌中随机抽 5 张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
- leetcode链接:https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/
思路
说了那么多,其实就是给一个数字数组,然后判断是否符合以下条件:
- 必须是五个数
- 大王小王可以跳过,所有数字只能出现1次
- 最大和最小的数相减必须少于5
/**
* @param {number[]} nums
* @return {boolean}
*/
var isStraight = function (nums) {
let min = Infinity
let max = -Infinity
const map = new Map()
for (const num of nums) {
if (num === 0) continue
if (!map.has(num)) {
map.set(num, true)
} else {
return false
}
max = Math.max(max, num)
min = Math.min(min, num)
}
return max - min < 5
}