当前位置:网站首页>[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
}
};
边栏推荐
- Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
- Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
- "21 days proficient in typescript-3" - install and build a typescript development environment md
- Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
- 国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
- PHP 严格模式
- Deep dive kotlin synergy (XXI): flow life cycle function
- Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
- Get ready for the pre-season card game MotoGP ignition champions!
- Desci: is decentralized science the new trend of Web3.0?
猜你喜欢
Android privacy sandbox developer preview 3: privacy, security and personalized experience
npm安装
Games101 notes (I)
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
ECU简介
Starkware: to build ZK "universe"
Seaborn draws 11 histograms
Domestic API management artifact used by the company
解决CMakeList find_package找不到Qt5,找不到ECM
How does win11 change icons for applications? Win11 method of changing icons for applications
随机推荐
Application of threshold homomorphic encryption in privacy Computing: Interpretation
Do sqlserver have any requirements for database performance when doing CDC
【 brosser le titre 】 chemise culturelle de l'usine d'oies
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
Jarvis OJ Flag
Dare not buy thinking
Pspnet | semantic segmentation and scene analysis
Iphone14 with pill screen may trigger a rush for Chinese consumers
Sentinel flow guard
Deep dive kotlin synergy (XXI): flow life cycle function
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
Games101 notes (III)
如何安装mysql
【微信小程序】一文读懂小程序的生命周期和路由跳转
How to uninstall MySQL cleanly
How to set the WiFi password of the router on the computer
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户