LeetCode61 扑克牌中的顺子

从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。

先除去0,然后再判断中间缺了多少个数,和0的数量比较就可以了。注意判断是否有相同的数。

 1 class Solution {
 2 public:
 3     bool isStraight(vector<int>& nums) {
 4         vector<int> temp;
 5         for(auto it=nums.begin();it!=nums.end();++it)
 6             if(*it!=0)
 7                 temp.push_back(*it);
 8         int len=temp.size();
 9         sort(temp.begin(),temp.end());
10         int diff=0;
11         for(auto it=temp.begin()+1;it!=temp.end();++it){
12             if(*it==*(it-1))
13                 return false;
14             diff+=*it-*(it-1)-1;
15         }
16         if(diff<=5-len)
17             return true;
18         return false;
19     }
20 };
原文地址:https://www.cnblogs.com/rookiez/p/13394229.html