当前位置:网站首页>【剑指 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的个数是否大于空缺个数
}
};
边栏推荐
- 中间表是如何被消灭的?
- How does win11 change icons for applications? Win11 method of changing icons for applications
- 清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会
- Facing new challenges and becoming a better self -- attacking technology er
- 【729. 我的日程安排表 I】
- Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
- Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
- Jarvis OJ webshell analysis
- Single merchant v4.4 has the same original intention and strength!
- 【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……
猜你喜欢

中间表是如何被消灭的?

Accès aux données - intégration du cadre d'entité

深潜Kotlin协程(二十一):Flow 生命周期函数

"21 days proficient in typescript-3" - install and build a typescript development environment md

Summary of methods for finding intersection of ordered linked list sets

Deep learning plus

Deep dive kotlin synergy (XXI): flow life cycle function

Cs231n notes (bottom) - applicable to 0 Foundation

How does win11 change icons for applications? Win11 method of changing icons for applications

Games101 notes (III)
随机推荐
中间表是如何被消灭的?
Jarvis OJ webshell analysis
Keras crash Guide
Jarvis OJ simple network management protocol
飞桨EasyDL实操范例:工业零件划痕自动识别
关于new Map( )还有哪些是你不知道的
[deep learning] how does deep learning affect operations research?
Hiengine: comparable to the local cloud native memory database engine
帮忙看看是什么问题可以吗?[ERROR] Could not execute SQL stateme
机器学习编译第2讲:张量程序抽象
StarkWare:欲构建ZK“宇宙”
Sentinel flow guard
Cheer yourself up
[echart] resize lodash to realize chart adaptation when window is zoomed
How can programmers improve their situation?
Flet教程之 09 NavigationRail 基础入门(教程含源码)
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
【刷题篇】鹅厂文化衫问题
Facing new challenges and becoming a better self -- attacking technology er