扑克牌的顺子

题目

https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof

解法

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
public boolean isStraight(int[] nums) {
if (nums==null || nums.length == 0) {
return true;
}
Arrays.sort(nums);
int joker = 0;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i] == 0) {
joker++;
}else if (nums[i] == nums[i+1]) {
return false;
}
}
return nums[4] - joker < 4;
}

public boolean isStraight2(int[] nums) {
if (nums==null || nums.length == 0) {
return true;
}
Set<Integer> res = new HashSet<>();
int max = 0, min = 14;
for (int num : nums) {
if (num == 0) {
continue;
}
max = Math.max(max, num);
min = Math.min(min, num);
if (res.contains(num)) {
return false;
}
res.add(num);
}
return max - min < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×