版权声明:本文自由转载,转载请注明出处。 https://blog.csdn.net/qq_38071429/article/details/84759983
import java.util.Arrays;
public class Solution {
public boolean isContinuous(int [] numbers) {
// 数组为空的情况
if(numbers.length==0) return false;
int count = 0, temp = 0, countKing = 0, first = 0;
boolean boo = true;
// 排序 代表大王的是0
Arrays.sort(numbers);
for(int i=0;i<numbers.length;i++){
// 计算大王的数量用countKing存储
if(numbers[i]==0){
countKing++;
count++; // 计算顺子的张数
first = i; // mark下最后一张大王的位置
continue;
}
else {
/*
* 数组中最大值减去非0最小值-1大于数组长度减去这两个数 够不成顺子
* 测试数据:0 1 3 0 7
* 排完序 0 0 1 3 7
* 需要2 4 5 6四个数才能构成顺子(六位顺子) 不可能
*/
/*
* 非零数字有重复 够不成顺子 测试数据:0 0 0 1 1
*/
if((numbers[numbers.length-1]-numbers[first+1]-1>numbers.length-2)
||((numbers[numbers.length-1]-numbers[first+1]==0)&&(first+1!=numbers.length-1))) break;
/*
* 没有大王的时候
* 测试数据:3 2 1 4 5
*/
if(boo==true){
temp = numbers[i];
count++;
boo = false;
continue;
}
temp += 1;
if(numbers[i]==temp) count++;
/*
* 使用大王可以代替任何牌的属性
* 测试数据:0 0 1 3 4
*/
if(numbers[i]!=temp&&countKing>0){
count++;
countKing--;
}
}
}
//System.out.println(count);
return count==numbers.length?true:false;
}
}