当前位置:网站首页>【剑指 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的个数是否大于空缺个数
}
};
边栏推荐
- 中间表是如何被消灭的?
- Data access - entityframework integration
- [es6] add if judgment or ternary operator judgment in the template string
- Accès aux données - intégration du cadre d'entité
- File operation --i/o
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- Cs231n notes (bottom) - applicable to 0 Foundation
- Today's sleep quality record 79 points
- sqlserver 做cdc 要对数据库性能有什么要求么
- [js] skill simplification if empty judgment
猜你喜欢
![[61dctf]fm](/img/22/3e4e3f1679a27d8b905684bb709905.png)
[61dctf]fm

Benji Banas membership pass holders' second quarter reward activities update list

采用药丸屏的iPhone14或引发中国消费者的热烈抢购

Starkware: to build ZK "universe"

DeSci:去中心化科学是Web3.0的新趋势?

Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found

Jarvis OJ simple network management protocol

飞桨EasyDL实操范例:工业零件划痕自动识别

文件操作--I/O

Win11如何给应用换图标?Win11给应用换图标的方法
随机推荐
[brush title] goose factory shirt problem
面对新的挑战,成为更好的自己--进击的技术er
【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……
如何将mysql卸载干净
【 brosser le titre 】 chemise culturelle de l'usine d'oies
"21 days proficient in typescript-3" - install and build a typescript development environment md
PSPNet | 语义分割及场景分析
Flet教程之 09 NavigationRail 基础入门(教程含源码)
You should have your own persistence
How was the middle table destroyed?
Today's sleep quality record 79 points
Get ready for the pre-season card game MotoGP ignition champions!
Benji Banas membership pass holders' second quarter reward activities update list
关于new Map( )还有哪些是你不知道的
tf. sequence_ Mask function explanation case
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Games101 notes (III)
HiEngine:可媲美本地的云原生内存数据库引擎
PHP 严格模式
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)