# 扑克牌的顺子

## 题目：从扑克牌中随机抽 5 张牌，判断是不是一个顺子， 即这 5 张牌是不是连续的。2～10 为数字本身， A 为 1。 J 为 11、Q 为 12、 为 13。小王可以看成任意数字。

### 算法实现

import java.util.Arrays;
public class Test44 {
/**
* 题目：从扑克牌中随机抽5张牌，判断是不是一个顺子， 即这5张牌是不是连续的。
* 2～10为数字本身， A为1。 J为11、Q为12、 为13。小王可以看成任意数字。
* @param numbers
* @return
*/
public static boolean isContinuous(int[] numbers) {
if (numbers == null || numbers.length != 5) {
return false;
}
// 对元素进行排序
Arrays.sort(numbers);
int numberOfZero = 0;
int numberOfGap = 0;
for (int i = 0; i < numbers.length && numbers[i] == 0; i++) {
numberOfZero++;
}
// 第一个非0元素的位置
int small = numberOfZero;
int big = small + 1;
while (big < numbers.length) {
if (numbers[small] == numbers[big]) {
return false;
}
numberOfGap += (numbers[big] - numbers[small] - 1);
small = big;
big++;
}
return numberOfGap <= numberOfZero;
}
public static void main(String[] args) {
int[] numbers1 = {1, 3, 2, 5, 4};
System.out.println(isContinuous(numbers1));
int[] numbers2 = {1, 3, 2, 6, 4};
System.out.println(isContinuous(numbers2));
int[] numbers3 = {0, 3, 2, 6, 4};
System.out.println(isContinuous(numbers3));
int[] numbers4 = {0, 3, 1, 6, 4};
System.out.println(isContinuous(numbers4));
int[] numbers5 = {1, 3, 0, 5, 0};
System.out.println(isContinuous(numbers5));
int[] numbers6 = {1, 3, 0, 7, 0};
System.out.println(isContinuous(numbers6));
int[] numbers7 = {1, 0, 0, 5, 0};
System.out.println(isContinuous(numbers7));
int[] numbers8 = {1, 0, 0, 7, 0};
System.out.println(isContinuous(numbers8));
int[] numbers9 = {3, 0, 0, 0, 0};
System.out.println(isContinuous(numbers9));
int[] numbers10 = {0, 0, 0, 0, 0};
System.out.println(isContinuous(numbers10));
int[] numbers11 = {1, 0, 0, 1, 0};
System.out.println(isContinuous(numbers11));
}
}