使用quartz框架實現定時任務
阿新 • • 發佈:2020-09-16
題目描述
從撲克牌中隨機抽5張牌,判斷是不是一個順子,即這5張牌是不是連續的。2~10為數字本身,A為1,J為11,Q為12,K為13,而大、小王為 0 ,可以看成任意數字。A 不能視為 14。
示例1:
輸入: [1,2,3,4,5]
輸出: True
示例2:
輸入: [0,0,1,2,5]
輸出: True
限制:
陣列長度為 5
陣列的數取值為 [0, 13] .
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof
思路解析
5張牌能否組成順子,需要滿足以下條件:
- 除0外其餘數字不能重複
- 最大值與最小值之差小於等於4
使用set
,依次插入非零元素,判斷set
的元素個數與零元素個數之和是否為5。
程式碼實現
class Solution { public: bool isStraight(vector<int>& nums) { int zero_num = 0; int max = 0; int min = 13; set<int> pokers; for(auto nt : nums) { if(nt) { pokers.insert(nt); max = (max > nt) ? max : nt; min = (min < nt) ? min : nt; } else zero_num++; } if(pokers.size() != nums.size() - zero_num) return false; if(max - min > 4) return false; return true; } };