当前位置:网站首页>[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
}
};
边栏推荐
- 【剑指 Offer】63. 股票的最大利润
- ECU introduction
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
- C# TCP如何限制单个客户端的访问流量
- Summary of PHP pseudo protocol of cisp-pte
- Timestamp strtotime the day before or after the date
- How was the middle table destroyed?
- Jarvis OJ shell流量分析
- tf. sequence_ Mask function explanation case
- The difference between searching forward index and inverted index
猜你喜欢
Jarvis OJ Webshell分析
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
composer安装报错:No composer.lock file present.
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
二叉树相关OJ题
Deep dive kotlin synergy (XXI): flow life cycle function
Pspnet | semantic segmentation and scene analysis
How to uninstall MySQL cleanly
随机推荐
Raspberry pie 4B installation pytorch1.11
【剑指 Offer】61. 扑克牌中的顺子
Games101 notes (III)
What is ROM
数据访问 - EntityFramework集成
[brush title] goose factory shirt problem
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
BS-XX-042 基于SSM实现人事管理系统
Learnopongl notes (II) - Lighting
Writing method of twig array merging
Iphone14 with pill screen may trigger a rush for Chinese consumers
Jarvis OJ shell traffic analysis
How does the outer disk futures platform distinguish formal security?
Deep dive kotlin synergy (XXI): flow life cycle function
How can C TCP set heartbeat packets to be elegant?
[deep learning] how does deep learning affect operations research?
Domestic API management artifact used by the company
【刷題篇】鹅廠文化衫問題
【剑指 Offer】62. 圆圈中最后剩下的数字