mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-11 19:17:05 +08:00
fix: check the rust codes and fix them (#653)
* fix: check the rust codes and fix it * Update binary_tree_bfs.rs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -34,7 +34,7 @@ pub fn main() {
|
||||
// 初始化哈希表
|
||||
let mut map = HashMap::new();
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
map.insert(*num, i);
|
||||
map.insert(*num, i); // key: 元素,value: 索引
|
||||
}
|
||||
let index = hashing_search_array(&map, target);
|
||||
println!("目标元素 3 的索引 = {}", index.unwrap());
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
fn linear_search_array(nums: &[i32], target: i32) -> i32 {
|
||||
// 遍历数组
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
// 找到目标元素,返回其索引
|
||||
if num == &target {
|
||||
return i as i32;
|
||||
}
|
||||
@@ -26,6 +27,7 @@ fn linear_search_array(nums: &[i32], target: i32) -> i32 {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user