-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbu-ke-pai-zhong-de-shun-zi-lcof.rs
69 lines (58 loc) · 1.55 KB
/
bu-ke-pai-zhong-de-shun-zi-lcof.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn is_straight1(nums: Vec<i32>) -> bool {
let mut nums = nums;
nums.sort();
let mut zero_num = 0;
for i in 0..nums.len() {
if nums[i] == 0 {
zero_num += 1;
} else {
if i > 0 && nums[i - 1] != 0 {
if nums[i] - nums[i - 1] > 0 {
zero_num -= (nums[i] - nums[i - 1] - 1);
} else {
return false;
}
if zero_num < 0 {
return false;
}
}
}
}
true
}
pub fn is_straight(nums: Vec<i32>) -> bool {
let mut min = std::i32::MAX;
let mut zero = 0; // 0 的数量
let set = nums
.into_iter()
.map(|x| {
if x == 0 {
zero += 1;
} else if x < min {
min = x;
}
x
})
.filter(|x| *x != 0)
.collect::<std::collections::HashSet<_>>();
if zero == 5 {
return true;
}
for i in 0..5 {
if set.contains(&(i + min)) {
continue;
} else {
if zero > 0 {
zero -= 1;
} else {
return false;
}
}
}
true
}
}