Files
hello-algo/ru/codes/rust/chapter_searching/linear_search.rs
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

55 lines
1.8 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* File: linear_search.rs
* Created Time: 2023-07-09
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::ListNode;
use std::cell::RefCell;
use std::rc::Rc;
/* Линейный поиск (массив) */
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);
}