mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-30 20:07:14 +00:00
zig : upgrade codes && rust : add codes for chapter_searching and chapter_dynamic_programming. (#591)
* zig : update zig codes * rust : add codes for linear_search and hashing_search * rust : add codes for linear_search and hashing_search * rust : add codes for chapter_dynamic_programming
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* File: hashing_search.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
|
||||
/* 哈希查找(数组) */
|
||||
fn hashing_search_array<'a>(map: &'a HashMap<i32, usize>, target: i32) -> Option<&'a usize> {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 None
|
||||
map.get(&target)
|
||||
}
|
||||
|
||||
/* 哈希查找(链表) */
|
||||
fn hashing_search_linked_list(map: &HashMap<i32, Rc<RefCell<ListNode<i32>>>>, target: i32) -> Option<&Rc<RefCell<ListNode<i32>>>> {
|
||||
// 哈希表的 key: 目标节点值,value: 节点对象
|
||||
// 若哈希表中无此 key ,返回 None
|
||||
map.get(&target)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 3;
|
||||
|
||||
/* 哈希查找(数组) */
|
||||
let nums = [ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 ];
|
||||
// 初始化哈希表
|
||||
let mut map = HashMap::new();
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
map.insert(*num, i);
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* File: linear_search.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
|
||||
/* 线性查找(数组) */
|
||||
fn linear_search_array(nums: &[i32], target: i32) -> i32 {
|
||||
// 遍历数组
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
if num == &target {
|
||||
return i as i32;
|
||||
}
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 线性查找(链表) */
|
||||
fn linear_search_linked_list(head: Rc<RefCell<ListNode<i32>>>, target: i32) -> Option<Rc<RefCell<ListNode<i32>>>> {
|
||||
// 找到目标节点,返回之
|
||||
if head.borrow().val == target {return Some(head)};
|
||||
if let Some(node) = &head.borrow_mut().next {
|
||||
return linear_search_linked_list(node.clone(), target);
|
||||
}
|
||||
// 未找到目标节点,返回 None
|
||||
return None;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 3;
|
||||
|
||||
/* 在数组中执行线性查找 */
|
||||
let nums = [ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 ];
|
||||
let index = linear_search_array(&nums, target);
|
||||
println!("目标元素 3 的索引 = {}", index);
|
||||
|
||||
/* 在链表中执行线性查找 */
|
||||
let head = ListNode::arr_to_linked_list(&nums);
|
||||
let node = linear_search_linked_list(head.unwrap(), target);
|
||||
println!("目标节点值 3 的对应节点对象为 {:?}", node);
|
||||
}
|
||||
Reference in New Issue
Block a user