-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucky-numbers-in-a-matrix.rs
59 lines (55 loc) · 1.36 KB
/
lucky-numbers-in-a-matrix.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
struct Solution;
impl Solution {
pub fn lucky_numbers(matrix: Vec<Vec<i32>>) -> Vec<i32> {
for v in matrix.iter() {
let mut ans = *v.first().unwrap();
let mut index = 0;
for (i, v) in v.iter().enumerate() {
if *v < ans {
ans = *v;
index = i;
}
}
let mut find = true;
for v in matrix.iter() {
if ans < v[index] {
find = false;
break;
}
}
if find {
return vec![ans];
}
}
vec![]
}
}
fn main() {
println!(
"{:?}",
Solution::lucky_numbers(vec![vec![3, 7, 8], vec![9, 11, 13], vec![15, 16, 17]])
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test_impl1() {
assert_eq!(
Solution::lucky_numbers(vec![vec![3, 7, 8], vec![9, 11, 13], vec![15, 16, 17]]),
vec![15]
);
assert_eq!(
Solution::lucky_numbers(vec![
vec![1, 10, 4, 2],
vec![9, 3, 8, 7],
vec![15, 16, 17, 12]
]),
vec![12]
);
assert_eq!(
Solution::lucky_numbers(vec![vec![7, 8], vec![1, 2]]),
vec![7]
);
}
}