当前位置:网站首页>[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
}
};
边栏推荐
- 深潜Kotlin协程(二十一):Flow 生命周期函数
- C how TCP restricts the access traffic of a single client
- Deep dive kotlin synergy (XXI): flow life cycle function
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- Facing new challenges and becoming a better self -- attacking technology er
- 挖财股票开户安全吗?怎么开股票账户是安全?
- 普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
- 采用药丸屏的iPhone14或引发中国消费者的热烈抢购
- [61dctf]fm
- Writing method of twig array merging
猜你喜欢
Deep dive kotlin synergy (XXI): flow life cycle function
[729. My schedule I]
中间表是如何被消灭的?
Cs231n notes (bottom) - applicable to 0 Foundation
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
Application of threshold homomorphic encryption in privacy Computing: Interpretation
用键盘输入一条命令
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
How to uninstall MySQL cleanly
[729. My Schedule i]
随机推荐
Do sqlserver have any requirements for database performance when doing CDC
Timestamp strtotime the day before or after the date
[brush title] goose factory shirt problem
[729. My schedule I]
Enter a command with the keyboard
What is the difference between EDI license and ICP business license
Jarvis OJ simple network management protocol
PHP talent recruitment system development source code recruitment website source code secondary development
How does the outer disk futures platform distinguish formal security?
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
Jarvis OJ Flag
[61dctf]fm
干货!半监督预训练对话模型 SPACE
Win11如何给应用换图标?Win11给应用换图标的方法
如何将mysql卸载干净
C# TCP如何限制单个客户端的访问流量
Writing method of twig array merging
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
Summary of PHP pseudo protocol of cisp-pte
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度