题目:
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
知识点:
判断
思路:
1、除了0,不能有重复的数
2、记录0的个数count,记录除0外的最大最小值
3、如果count=5,返回false;如果count=4.返回true;如果count=3,最大值最小值之差大于1,小于4;如果count=2,最大值最小值之差大于2,小于4;如果count=1,最大值最小值之差大于3,小于4;如果count=1,最大值最小值之等于4。满足以上条件结果返回为true,否则返回false。
答案:
public class Solution {
public boolean isContinuous(int [] numbers) {
if(numbers.length==0){return false;}
int count = 0;
int min = numbers[0];
int max = numbers[0];
for(int i=0;i<numbers.length;i++){
if(numbers[i]!=0){min = numbers[i];}
for(int j=i+1;j<numbers.length;j++){
if(numbers[i]!=0 && numbers[i]==numbers[j]){return false;}
}
}
for(int i=0;i<numbers.length;i++){
if(numbers[i]==0){count++;}
if(numbers[i]!=0 && numbers[i]<min){min = numbers[i];}
if(numbers[i]!=0 && numbers[i]>max){max = numbers[i];}
}
int tmp = max-min;
if(count==5){return false;}
if(count==4){return true;}
if(count==3){
if(tmp>=1&&tmp<=4){
return true;
}else{
return false;
}
}
if(count==2){
if(tmp>=2&&tmp<=4){
return true;
}else{
return false;
}
}
if(count==1){
if(tmp>=3&&tmp<=4){
return true;
}else{
return false;
}
}
if(count==0){
if(tmp==4){
return true;
}else{
return false;
}
}
return true;
}
}