# [sword finger Offer learning] [question 45: playing card shunzi]

Title:
LL is in A good mood today, because he bought A deck of playing cards and found that there are two kings and two Xiao Wang in it He randomly took out five cards from them and wanted to test his luck to see if he could get A good luck. If he did, he decided to buy sports lottery, hehe!! "Hearts A, spades 3, Wang, Wang, square 5", "Oh My God!" is not shunzi LL is not happy. He thinks about it. He decides that big / small Wang can be regarded as any number, and A is regarded as 1,J as 11,Q as 12, and K as 13. The five cards above can be changed into "1,2,3,4,5" (big and small kings are regarded as 2 and 4 respectively), "So Lucky!". LL decided to buy A sports lottery Now, you are required to use this card to simulate the above process, and then tell us how lucky LL is. If the card can be formed into A shunzi, it will output true, otherwise it will output false. For the sake of convenience, you can think of big and small king as 0.

Train of thought:
1. Use quick sorting to sort 5 numbers;
2. Use zeroCount to count the number of zeros in the array, and use count to count the number of zeros between two adjacent numbers in the array;
3. Judge the size of zeroCount and count. If zeroCount is greater than or equal to count, return true; otherwise, return false.

Procedure:

```public class subject45 {
public static boolean isContinuous(int[] numbers) {
if(numbers.length < 5 || numbers == null) {
return false;
}
int[] arr = sort(numbers, 0, numbers.length - 1);
int zeroCount = 0;//Number of records 0
int count = 0;//Record the number of zeros between adjacent numbers
for(int i = 0; i < numbers.length; i ++) {
if(arr[i] == 0) {
zeroCount ++;
}
if(i < numbers.length - 1 && numbers[i] != 0 && numbers[i + 1] != 0) {
if(numbers[i] == numbers[i + 1]) {//Returns false if two numbers are equal
return false;
}
count += arr[i + 1] - arr[i] - 1;
}
}
return zeroCount >= count ? true : false;
}
public static int[] sort(int[] array, int start, int end) {//Quick sort
if(array.length == 0 || start >= end) {
return array;
}
int pivot = array[start];
int left = start;
int right = end;
while(left < right) {
while(left < right && array[right] > pivot) {
right --;
}
if(left < right) {
array[left] = array[right];
left ++;
}
while(left < right && array[left] < pivot) {
left ++;
}
if(left < right) {
array[right] = array[left];
right --;
}
}
array[left] = pivot;
sort(array, 0, left - 1);
sort(array, left + 1, end);
return array;
}
public static void main(String args[]) {
int[] arr = new int[] {0,3,2,6,4};
System.out.println(isContinuous(arr));
}
}
```

Posted on Sun, 10 Nov 2019 15:40:11 -0500 by pranav_kavi