-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinding-3-digit-even-numbers.rs
62 lines (55 loc) · 1.76 KB
/
finding-3-digit-even-numbers.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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
/// 枚举100-999的数据
pub fn find_even_numbers(digits: Vec<i32>) -> Vec<i32> {
let mut hash = std::collections::HashMap::new();
for i in digits {
hash.entry(i).and_modify(|x| *x += 1).or_insert(1);
}
let mut result = vec![];
for j in 100..=999 {
if j % 2 == 1 {
continue;
}
let x = j / 100;
let y = j / 10 % 10;
let z = j % 10;
if x != y && y != z && x != z {
if hash.contains_key(&x) && hash.contains_key(&y) && hash.contains_key(&z) {
result.push(j);
}
} else if x == y && y != z {
match (hash.get(&x), hash.get(&z)) {
(Some(m), Some(_)) if *m >= 2 => {
result.push(j);
}
_ => {}
}
} else if x == z && z != y {
match (hash.get(&x), hash.get(&y)) {
(Some(m), Some(_)) if *m >= 2 => {
result.push(j);
}
_ => {}
}
} else if y == z && z != x {
match (hash.get(&z), hash.get(&x)) {
(Some(m), Some(_)) if *m >= 2 => {
result.push(j);
}
_ => {}
}
} else {
match hash.get(&x) {
Some(m) if *m >= 3 => {
result.push(j);
}
_ => {}
}
}
}
result
}
}