当前位置:网站首页>[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
}
};
边栏推荐
- 养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
- Etcd build a highly available etcd cluster
- 深耕5G,芯讯通持续推动5G应用百花齐放
- 树莓派4b安装Pytorch1.11
- Jarvis OJ shell流量分析
- Sentinel-流量防卫兵
- Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- 精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
- Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
- [deep learning] [original] let yolov6-0.1.0 support the txt reading dataset mode of yolov5
猜你喜欢
随机推荐
Practical example of propeller easydl: automatic scratch recognition of industrial parts
Raspberry pie 4B installation pytorch1.11
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
关于new Map( )还有哪些是你不知道的
Hiengine: comparable to the local cloud native memory database engine
[wechat applet] read the life cycle and route jump of the applet
中间表是如何被消灭的?
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Domestic API management artifact used by the company
Learnopongl notes (I)
Facing new challenges and becoming a better self -- attacking technology er
面对新的挑战,成为更好的自己--进击的技术er
Bs-xx-042 implementation of personnel management system based on SSM
【剑指 Offer】62. 圆圈中最后剩下的数字
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
How to set the WiFi password of the router on the computer
WSL2.0安装
Writing method of twig array merging
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)