当前位置:网站首页>[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
}
};
边栏推荐
- [echart] resize lodash to realize chart adaptation when window is zoomed
- [61dctf]fm
- The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
- PHP 严格模式
- [deep learning] how does deep learning affect operations research?
- C how TCP restricts the access traffic of a single client
- [es6] add if judgment or ternary operator judgment in the template string
- [echart] resize lodash 实现窗口缩放时图表自适应
- Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
- 中间表是如何被消灭的?
猜你喜欢

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

【刷題篇】鹅廠文化衫問題

Jarvis OJ shell traffic analysis

干货!半监督预训练对话模型 SPACE

Pspnet | semantic segmentation and scene analysis

Jarvis OJ Telnet Protocol

Summary of PHP pseudo protocol of cisp-pte

美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名

【刷题篇】有效的数独

【刷题篇】鹅厂文化衫问题
随机推荐
Jarvis OJ shell traffic analysis
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
Learnopongl notes (I)
用键盘输入一条命令
[js] skill simplification if empty judgment
How to uninstall MySQL cleanly
齐宣王典故
ECU简介
【剑指 Offer】63. 股票的最大利润
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
[729. My schedule I]
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Summary of PHP pseudo protocol of cisp-pte
如何安装mysql
Cs231n notes (bottom) - applicable to 0 Foundation
File operation --i/o
Android privacy sandbox developer preview 3: privacy, security and personalized experience
Jarvis OJ simple network management protocol
Jarvis OJ Flag
C# TCP如何限制单个客户端的访问流量