剑指offer(45)扑克牌顺子
import java.util.Arrays;
import java.util.Scanner;
//看拿出来的一把牌是否是顺子,A:1,JQK:11,12,13 大王小王:0,可看作任何数字
//先判断numbers的长度,为0直接返回false
//求0的个数
//求两个相邻数字之间的空位
//如果有对子直接返回false
//如果0的个数大于等于相邻数字之间的空位则返回true
public class NoFourtyfive {
public boolean isContinuous(int[] numbers){
int length = numbers.length;
if(length == 0){
return false;
}
int numsOfZero = 0;
int numsOfDis = 0;
Arrays.sort(numbers);
for(int i = 0;i < length - 1;i++){
if(numbers[i] == 0){
numsOfZero++;
continue;
}
if(numbers[i] == numbers[i + 1]){
return false;
}
numsOfDis += numbers[i + 1] - numbers[i] - 1;
}
if(numsOfZero >= numsOfDis){
return true;
}
return false;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String arr = sc.next();
String[] arr1 = arr.split(",");
int[] arrTer = new int[arr1.length];
for(int i = 0;i < arrTer.length;i++){
arrTer[i] = Integer.parseInt(arr1[i]);
}
NoFourtyfive fourtyfive = new NoFourtyfive();
boolean b = fourtyfive.isContinuous(arrTer);
System.out.println(b);
}
}