当前位置:网站首页>【剑指 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的个数是否大于空缺个数
}
};
边栏推荐
- 【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
- Accès aux données - intégration du cadre d'entité
- 【微信小程序】一文读懂小程序的生命周期和路由跳转
- Jarvis OJ shell流量分析
- Enter a command with the keyboard
- Flet教程之 12 Stack 重叠组建图文混合 基础入门(教程含源码)
- 浏览器渲染原理以及重排与重绘
- 采用药丸屏的iPhone14或引发中国消费者的热烈抢购
- PHP人才招聘系统开发 源代码 招聘网站源码二次开发
- Combined use of vant popup+ other components and pit avoidance Guide
猜你喜欢

Flet教程之 12 Stack 重叠组建图文混合 基础入门(教程含源码)

Domestic API management artifact used by the company

Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)

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

用键盘输入一条命令

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

Jarvis OJ simple network management protocol

养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪

HiEngine:可媲美本地的云原生内存数据库引擎

Enter a command with the keyboard
随机推荐
怎样在电脑上设置路由器的WiFi密码
[brush title] goose factory shirt problem
Games101 notes (II)
Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
[深度学习][原创]让yolov6-0.1.0支持yolov5的txt读取数据集模式
tf. sequence_ Mask function explanation case
npm安装
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
[brush questions] effective Sudoku
Flet教程之 09 NavigationRail 基础入门(教程含源码)
How was the middle table destroyed?
Yarn common commands
Games101 notes (I)
网站页面禁止复制内容 JS代码
HiEngine:可媲美本地的云原生内存数据库引擎
Jarvis OJ shell traffic analysis
拷贝方式之DMA
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
[729. My Schedule i]