forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
42 lines (37 loc) · 870 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Arrays;
/**
* @author Anonymous
* @since 2019/12/12
*/
class Solution {
/**
* 判断是否是连续的数字
*
* @param numbers 数组
* @return 是否是顺子
*/
public boolean isContinuous(int[] numbers) {
if (numbers == null || numbers.length == 0) {
return false;
}
int zeroCount = 0;
Arrays.sort(numbers);
for (int e : numbers) {
if (e > 0) {
break;
}
++zeroCount;
}
int p = zeroCount, q = p + 1, n = numbers.length;
int gap = 0;
while (q < n) {
if (numbers[p] == numbers[q]) {
return false;
}
gap += (numbers[q] - numbers[p] - 1);
p = q;
++q;
}
return gap <= zeroCount;
}
}