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
| use std::collections::HashMap; use std::collections::HashSet;
fn search_m<'a>(hashmap: HashMap<&str, Vec<&'a str>>) -> Option<&'a str> { let mut queue: Vec<&str> = Vec::new(); let mut check_list = HashSet::new(); if let Some(first) = hashmap.get("you") { for ele in first { queue.push(ele); } } while queue.len() > 0 { let c = queue.remove(0); if check_list.contains(c) { continue; } check_list.insert(c); if c.ends_with("m") { return Some(c); } if let Some(eles) = hashmap.get(c) { for ele in eles { queue.push(ele) } } } None }
fn main() { let mut hashmap = HashMap::new(); hashmap.insert("you", vec!["alice", "bob", "claire"]); hashmap.insert("bob", vec!["anuj", "peggy"]); hashmap.insert("alice", vec!["peggy"]); hashmap.insert("claire", vec!["you", "thom", "jonny"]); hashmap.insert("anuj", vec![]); hashmap.insert("peggy", vec![]); hashmap.insert("thom", vec![]); hashmap.insert("jonny", vec![]);
let v = search_m(hashmap); match v { Some(i) => { println!("find mongoose! name {:?}", i); } None => { println!("not find mongoose"); } } }
|