2023-07-10 01:32:12 +08:00
|
|
|
|
/*
|
|
|
|
|
* File: hashing_search.rs
|
|
|
|
|
* Created Time: 2023-07-09
|
2024-01-02 01:25:37 +08:00
|
|
|
|
* Author: codingonion (coderonion@gmail.com)
|
2023-07-10 01:32:12 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2024-10-23 18:49:47 +08:00
|
|
|
|
use hello_algo_rust::include::ListNode;
|
2024-03-16 02:13:41 +08:00
|
|
|
|
use std::cell::RefCell;
|
2023-09-02 23:54:38 +08:00
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::rc::Rc;
|
2023-07-10 01:32:12 +08:00
|
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
|
|
|
|
fn hashing_search_array<'a>(map: &'a HashMap<i32, usize>, target: i32) -> Option<&'a usize> {
|
|
|
|
|
// 哈希表的 key: 目标元素,value: 索引
|
|
|
|
|
// 若哈希表中无此 key ,返回 None
|
|
|
|
|
map.get(&target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(链表) */
|
2024-03-16 02:13:41 +08:00
|
|
|
|
fn hashing_search_linked_list(
|
|
|
|
|
map: &HashMap<i32, Rc<RefCell<ListNode<i32>>>>,
|
|
|
|
|
target: i32,
|
|
|
|
|
) -> Option<&Rc<RefCell<ListNode<i32>>>> {
|
2023-07-10 01:32:12 +08:00
|
|
|
|
// 哈希表的 key: 目标节点值,value: 节点对象
|
|
|
|
|
// 若哈希表中无此 key ,返回 None
|
|
|
|
|
map.get(&target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
pub fn main() {
|
|
|
|
|
let target = 3;
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
2024-03-16 02:13:41 +08:00
|
|
|
|
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
2023-07-10 01:32:12 +08:00
|
|
|
|
// 初始化哈希表
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
for (i, num) in nums.iter().enumerate() {
|
2023-07-24 22:27:26 +08:00
|
|
|
|
map.insert(*num, i); // key: 元素,value: 索引
|
2023-07-10 01:32:12 +08:00
|
|
|
|
}
|
|
|
|
|
let index = hashing_search_array(&map, target);
|
|
|
|
|
println!("目标元素 3 的索引 = {}", index.unwrap());
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(链表) */
|
|
|
|
|
let head = ListNode::arr_to_linked_list(&nums);
|
|
|
|
|
// 初始化哈希表
|
|
|
|
|
// let mut map1 = HashMap::new();
|
|
|
|
|
let map1 = ListNode::linked_list_to_hashmap(head);
|
|
|
|
|
let node = hashing_search_linked_list(&map1, target);
|
|
|
|
|
println!("目标节点值 3 的对应节点对象为 {:?}", node);
|
|
|
|
|
}
|