forked from xurui1995/Sword-pointing-to-offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
No44.java
40 lines (31 loc) · 822 Bytes
/
No44.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
import java.util.Arrays;
public class No44 {
/**
* 从扑克牌中随机抽5张牌,判断是不是一个顺子,
* 即这5张牌是不是连续的。2~10为数字本身,
* A为1,J为11,Q为12,K为13,而大、小王可以看成任意数字
*/
public static void main(String[] args) {
int[] array={0,4,6,8,0};
System.out.println(isContinuous(array));
}
private static boolean isContinuous(int[] arr) {
if(arr == null || arr.length!= 5)
return false;
Arrays.sort(arr);
int numberZero=0;
int numberGap=0;
for(int i=0;i<arr.length&&arr[i]==0;i++)
numberZero++;
int small=numberZero;
int big=small+1;
while(big<arr.length){
if(arr[small]==arr[big])
return false;
numberGap+=arr[big]-arr[small]-1;
small=big;
big++;
}
return (numberGap>numberZero)?false:true;
}
}