当前位置:网站首页>【剑指 Offer】61. 扑克牌中的顺子
【剑指 Offer】61. 扑克牌中的顺子
2022-07-05 16:22:00 【LuZhouShiLi】
剑指 Offer 61. 扑克牌中的顺子
题目
从若干副扑克牌中随机抽 5 张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
思路
三件事
- 首先将数组进行排序
- 统计数组中0的个数
- 最后统计排序之后的数组中相邻数字之间空缺的总数
- 判断0的个数是否大于或者等于空缺总数
代码
class Solution {
public:
bool isStraight(vector<int>& nums) {
sort(nums.begin(),nums.end());// 排序
int king = 0;
int gap = 0;
for(int i = 0; i < nums.size(); ++i)
{
if(nums[i] == 0)
{
++king;// 统计0的个数
}
if(i > 0 && nums[i] != 0 && nums[i] == nums[i - 1])
{
return false;// 遇到相同的数字
}
if(i > 0 && nums[i - 1] != 0 && nums[i] > nums[i - 1] + 1)
{
gap += nums[i] - nums[i - 1] - 1;// 计算空缺 记得减一
}
}
return king >= gap;//判断0的个数是否大于空缺个数
}
};
边栏推荐
- Writing method of twig array merging
- 【深度学习】深度学习如何影响运筹学?
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- StarkWare:欲构建ZK“宇宙”
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 【机器人坐标系第一讲】
- 挖财股票开户安全吗?怎么开股票账户是安全?
- Some cognitive thinking
- 国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
- Games101 notes (III)
猜你喜欢
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
Bs-xx-042 implementation of personnel management system based on SSM
二叉树相关OJ题
Games101 notes (III)
How was the middle table destroyed?
Summary of methods for finding intersection of ordered linked list sets
[61dctf]fm
The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
【刷题篇】有效的数独
[deep learning] how does deep learning affect operations research?
随机推荐
为季前卡牌游戏 MotoGP Ignition Champions 做好准备!
[es6] 模板字符串内添加if判断或添加三元运算符判断
What is the difference between EDI license and ICP business license
DeSci:去中心化科学是Web3.0的新趋势?
Bs-xx-042 implementation of personnel management system based on SSM
China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
树莓派4b安装Pytorch1.11
[deep learning] how does deep learning affect operations research?
Enter a command with the keyboard
How to uninstall MySQL cleanly
Solution of vant tabbar blocking content
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
时间戳strtotime前一天或后一天的日期
如何安装mysql
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
[js] 技巧 简化if 判空
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
2020-2022 two-year anniversary of creation
How to set the WiFi password of the router on the computer