当前位置:网站首页>[Jianzhi offer] 61 Shunzi in playing cards
[Jianzhi offer] 61 Shunzi in playing cards
2022-07-05 16:58:00 【LuZhouShiLi】
The finger of the sword Offer 61. Shunzi in playing cards
subject
Draw at random from several sets of playing cards 5 card , Judge whether it's a down son , Is this 5 Are cards continuous .2~10 For the number itself ,A by 1,J by 11,Q by 12,K by 13, And big 、 Wang Wei 0 , It can be seen as any number .A Can't be regarded as 14.
Ideas
Three things
- First, sort the array
- In the statistics array 0 The number of
- Finally, count the total number of vacancies between adjacent numbers in the sorted array
- Judge 0 Whether the number of is greater than or equal to the total number of vacancies
Code
class Solution {
public:
bool isStraight(vector<int>& nums) {
sort(nums.begin(),nums.end());// Sort
int king = 0;
int gap = 0;
for(int i = 0; i < nums.size(); ++i)
{
if(nums[i] == 0)
{
++king;// Statistics 0 The number of
}
if(i > 0 && nums[i] != 0 && nums[i] == nums[i - 1])
{
return false;// Encounter the same number
}
if(i > 0 && nums[i - 1] != 0 && nums[i] > nums[i - 1] + 1)
{
gap += nums[i] - nums[i - 1] - 1;// Calculate vacancy Remember to subtract one
}
}
return king >= gap;// Judge 0 Whether the number of is greater than the number of vacancies
}
};
边栏推荐
- 麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 如何安装mysql
- Benji Banas membership pass holders' second quarter reward activities update list
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- 挖财股票开户安全吗?怎么开股票账户是安全?
- 帮忙看看是什么问题可以吗?[ERROR] Could not execute SQL stateme
- 机器学习编译第2讲:张量程序抽象
- Jarvis OJ Flag
- 高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
猜你喜欢
随机推荐
Data verification before and after JSON to map -- custom UDF
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
How to set the WiFi password of the router on the computer
Summary of PHP pseudo protocol of cisp-pte
Win11提示无法安全下载软件怎么办?Win11无法安全下载软件
Sentinel flow guard
文件操作--I/O
二叉树相关OJ题
树莓派4b安装Pytorch1.11
Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
【微信小程序】一文读懂小程序的生命周期和路由跳转
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
Dare not buy thinking
Jarvis OJ simple network management protocol
[61dctf]fm
The difference between searching forward index and inverted index
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
深耕5G,芯讯通持续推动5G应用百花齐放
Bs-xx-042 implementation of personnel management system based on SSM







