package com.company.test.扑克牌顺子;
import java.util.*;
/**
* @author xienl
* @description 扑克牌顺子
* @date 2022/7/8
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[] arr = {1,2,3,4,6};
System.out.println(solution.IsContinuous(arr));
}
public boolean IsContinuous(int [] numbers) {
Arrays.sort(numbers);
int n = 0;
int left = 0, right = numbers.length - 1 ;
while (left < right){
if (numbers[left] == 0){
n++;
left++;
continue;
}
if (numbers[left] + 1 == numbers[left + 1]){
left++;
continue;
}
if (numbers[left] + 1 < numbers[left + 1]){
if (n == 0){
return false;
}
while (n > 0 && numbers[left] + 1 != numbers[left + 1]){
numbers[left] = numbers[left] + 1;
n--;
}
continue;
} else {
return false;
}
}
return true;
}
public boolean IsContinuous2(int [] numbers) {
Set<Integer> set = new HashSet<>();
int num = 0;
int min = 13, max = 0;
for (int i = 0; i < numbers.length; i++){
if (numbers[i] > 0){
if (set.contains(numbers[i])){
return false;
}
set.add(numbers[i]);
if (numbers[i] <= min){
min = numbers[i];
}
if (numbers[i] >= max){
max = numbers[i];
}
}
}
return max - min < 5;
}
}
04-29
306
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-02
2377
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-21
297
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)