forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test44.java
73 lines (63 loc) · 2.17 KB
/
Test44.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Arrays;
/**
* Author: 王俊超
* Date: 2015-06-14
* Time: 14:33
* Declaration: All Rights Reserved !!!
*/
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));
}
}