-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-center-of-star-graph.rs
90 lines (81 loc) · 2.1 KB
/
find-center-of-star-graph.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
struct Solution;
impl Solution {
fn impl1(edges: Vec<Vec<i32>>) -> i32 {
use std::collections::HashMap;
let mut map: HashMap<i32, i32> = HashMap::new();
let len = edges.len() as i32;
for v in edges {
for i in v {
if let Some(m) = map.get_mut(&i) {
*m += 1;
} else {
map.insert(i, 1);
}
}
}
for (k, v) in map {
if v == len {
return k;
}
}
0
}
#[allow(unused)]
fn impl2(edges: Vec<Vec<i32>>) -> i32 {
let first = &edges[0];
let second = &edges[1];
for i in first {
if second.contains(i) {
return *i;
}
}
0
}
#[allow(unused)]
fn impl3(edges: Vec<Vec<i32>>) -> i32 {
if edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] {
edges[0][0]
} else if edges[0][1] == edges[1][0] || edges[0][1] == edges[1][1] {
edges[0][1]
} else {
0
}
}
pub fn find_center(edges: Vec<Vec<i32>>) -> i32 {
Solution::impl1(edges)
}
}
fn main() {
println!(
"{}",
Solution::find_center(vec![vec![1, 2], vec![2, 3], vec![4, 2]])
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test_impl1() {
assert_eq!(Solution::impl1(vec![vec![1, 2], vec![2, 3], vec![4, 2]]), 2);
assert_eq!(
Solution::impl1(vec![vec![1, 2], vec![5, 1], vec![1, 3], vec![1, 4]]),
1
);
}
#[test]
fn test_impl2() {
assert_eq!(Solution::impl2(vec![vec![1, 2], vec![2, 3], vec![4, 2]]), 2);
assert_eq!(
Solution::impl2(vec![vec![1, 2], vec![5, 1], vec![1, 3], vec![1, 4]]),
1
);
}
#[test]
fn test_impl3() {
assert_eq!(Solution::impl3(vec![vec![1, 2], vec![2, 3], vec![4, 2]]), 2);
assert_eq!(
Solution::impl3(vec![vec![1, 2], vec![5, 1], vec![1, 3], vec![1, 4]]),
1
);
}
}