剑指offer——61. 扑克牌顺子

61. 扑克牌顺子

题目链接

NowCoder

题目描述

五张牌,其中大小鬼为癞子,牌面为 0。判断这五张牌是否能组成顺子。


解题思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
O(nlogn)

**/
class Solution {
public:
bool IsContinuous( vector<int> num ) {
if(num.empty()) return false;
//排序
sort(num.begin(), num.end());
//统计0的个数
int index = 0;
int zeroNum = 0;
while(num[index] == 0)
{
index ++;
zeroNum ++;
}
#if 0
//统计空格数目
int spaceNum = 0;
for(int i = index; i < num.size() - 1; i ++)
{
int space = num[i+1] - num[i];
if(0 == space) return false;
spaceNum += space - 1;
if(spaceNum > zeroNum) return false;
}
#endif
//直接判断
return true;
}
};
创作不易,欢迎打赏!
-------------本文结束感谢您的阅读-------------