思路1:一个一个讨论0的个数,然后判断
# -*- coding:utf-8 -*-
class Solution:
def IsContinuous(self, numbers):
# write code here
if not numbers:
return False
numbers=sorted(numbers)
if 0 in numbers:
nums=numbers.count(0)
if nums == 1:
if numbers[4]-numbers[1]<=4 and len(set(numbers))==5:
return True
return False
if nums==2:
if numbers[4]-numbers[2]<=4 and len(set(numbers))==4:
return True
return False
if nums==3:
if numbers[4]-numbers[3]<=4 and len(set(numbers))==3:
return True
return False
if nums==4:
return True
if numbers[4]-numbers[0]<=4 and len(set(numbers))==5:
return True
return False
思路2:首先去除0开始排序,
如果一个0没有,那么cnt=4
一个0,cnt=3,4
2个0,cnt=2,3,4
3个0,cnt=1,2,3,4
4个0 ,cnt=0
都是小于等于4
if not numbers:
return False
tmp=[i for i in numbers if i>0]
tmp=sorted(tmp)
cnt=0
for i in range(1,len(tmp)):
if tmp[i]-tmp[i-1]>0:
cnt+=tmp[i]-tmp[i-1]
else:
return False
if cnt<=4:
return True
return False