题解 | #扑克牌顺子#
缺失的第一个正整数
http://www.nowcoder.com/practice/50ec6a5b0e4e45348544348278cdcee5
import java.util.*;
public class Solution {
public boolean IsContinuous(int [] numbers) {
Arrays.sort(numbers);
int cnt=0;
int k=0;
for(int i=0;i<numbers.length;i++){
if(numbers[i]==0){
cnt++;
}
//else{
// k=i;
//}
}
if(cnt==4){
return true;
}
else if(cnt==3){
if(numbers[numbers.length-1]!=numbers[numbers.length-2]&&numbers[numbers.length-1]-numbers[numbers.length-2]<=4)
return true;
else{
return false;
}
}
else if(cnt==0){//numbers[0]!=0
if(numbers[numbers.length-1]-numbers[0]!=4)return false;
else{
for(int i=0;i<numbers.length-1;i++){
if(numbers[i]+1!=numbers[i+1])return false;
}
}
}
else if(cnt==1){//numbers[0]==0&&numbers[1]!=0
for(int i=1;i<numbers.length-1;i++){//
if(numbers[i+1]-numbers[i]<=2&&numbers[i+1]!=numbers[i]&&numbers[numbers.length-1]-numbers[1]<=4){//&&numbers[i]+1==numbers[i+1]
//cnt--;
return true;
}else{
return false;
}
}
//if(cnt<0){
//return false;}
}
else if(cnt==2){//numbers[0]==0&&numbers[1]==0&&numbers[2]!=0
for(int i=2;i<numbers.length-1;i++){
if(numbers[i+1]-numbers[i]<=3&&numbers[i+1]!=numbers[i]&&numbers[numbers.length-1]-numbers[2]<=4){//&&numbers[i]+1!=numbers[i+1]
//cnt--;
return true;
}else{
return false;
}
}
//if(cnt<0){
//return false;
//}
}
/*for(;k<numbers.length;k++){
if(numbers[k]+1!=numbers[k+1]){
cnt--;
}
}*/
//int a2=0;
//1->0
//2->0
//if(numbers[0]==0&&numbers[1]!=0){}
return true;
}
}