We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaScript实现LeetCode第61题:扑克牌中的顺子
从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
示例 1: 输入: [1,2,3,4,5] 输出: True 示例 2: 输入: [0,0,1,2,5] 输出: True
限制: 1.数组长度为 5 2.数组的数取值为 [0, 13] .
wangNums
grapNums
nums[i]
wangNums++
nums[i] == nums[i+1]
/** * @param {number[]} nums * @return {boolean} */ var isStraight = function(nums) { if(nums.length != 5) { return false; } nums.sort((a, b) => a - b); let wangNums = 0; // 王的个数 let grapNums = 0; // 排序后元素的差值 for(let i = 0; i < nums.length - 1; i++) { if(nums[i] == 0) { wangNums++; } else if(nums[i] == nums[i+1]) { //不是王,并且还是对子,那肯定不是顺子了 return false; } else { //不是王,计算一下两两的差值,最后与王的个数做比较 grapNums += (nums[i+1] - nums[i] - 1); } } //差值小于王的个数,说明可以用王来构成顺子 if(grapNums <= wangNums) { return true; } return false; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
JavaScript实现LeetCode第61题:扑克牌中的顺子
题目描述
从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
限制:
1.数组长度为 5
2.数组的数取值为 [0, 13] .
思路分析
wangNums
,�用来存储王的�个数,一个为grapNums
,用来存储 排序后元素的差值nums[i]
如果值为0,则wangNums++
,如果nums[i] == nums[i+1]
则直接返回false, 其他�情况,计算差值,�累加�到grapNums
grapNums
)小于王(wangNums
)的个数,说明可以用王来构成顺子,返回true,否则返回��false解法
The text was updated successfully, but these errors were encountered: