-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path扑克牌的顺子.py
42 lines (37 loc) · 1.23 KB
/
扑克牌的顺子.py
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
34
35
36
37
38
39
40
41
42
'''
随机从扑克牌中抽出了5张牌,判断是不是顺子,
决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。
'''
# -*- coding:utf-8 -*-
class Solution:
def IsContinuous(self, numbers):
if numbers == None or len(numbers) <= 0:
return False
# 把A、J、Q、K转化一下
transDict = {'A': 1, 'J': 11, 'Q': 12, 'K': 13}
for i in range(len(numbers)):
if numbers[i] in transDict.keys():
numbers[i] = transDict[numbers[i]]
numbers = sorted(numbers)
numberOfzero = 0
numberOfGap = 0
# 统计0的个数
i = 0
while i < len(numbers) and numbers[i] == 0:
numberOfzero += 1
i += 1
# 统计间隔的数目
small = numberOfzero
big = small + 1
while big < len(numbers):
# 出现对子, 不可能是顺子
if numbers[small] == numbers[big]:
return False
numberOfGap += numbers[big] - numbers[small] - 1
small = big
big += 1
return False if numberOfGap > numberOfzero else True
test = ['A', 3, 2, 5, 0]
test2 = [0, 3, 1, 6, 4]
s = Solution()
print(s.IsContinuous(test2))