mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
update rust codes for hash_map, binary_search, bubble_sort, stack, queue (#330)
* update rust codes * update rust codes * update rust codes * update and add rust codes for hash_map, binary_search, bubble_sort * update and add rust codes for hash_map, binary_search, bubble_sort * add rust codes for chapter stack * add rust codes for chapter queue * add rust codes for chapter deque
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
[package]
|
||||
name = "chapter_array_and_linkedlist"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "array"
|
||||
path = "array.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "list"
|
||||
path = "list.rs"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* File: array.rs
|
||||
* Created Time: 2023-01-15
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
/* 随机返回一个数组元素 */
|
||||
@@ -15,7 +15,7 @@ fn random_access(nums: &[i32]) -> i32 {
|
||||
|
||||
/* 扩展数组长度 */
|
||||
fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
|
||||
// 创建一个长度为 nums.len() + enlarge 的新 Vec
|
||||
// 初始化一个扩展长度后的数组
|
||||
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
|
||||
// 将原数组中的所有元素复制到新
|
||||
for i in 0..nums.len() {
|
||||
@@ -70,33 +70,38 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let arr = [0; 5];
|
||||
println!("数组 arr = {:?}", arr);
|
||||
print!("数组 arr = ");
|
||||
inc::print_util::print_array(&arr);
|
||||
// 在 Rust 中,指定长度时([i32; 5])为数组
|
||||
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
|
||||
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组(Array)也是rust一般情况下使用动态数组的类型
|
||||
let nums = vec![1, 3, 2, 5, 4];
|
||||
println!("数组 nums = {:?}", nums);
|
||||
let nums = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("\n数组 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
|
||||
/* 随机访问 */
|
||||
let random_num = random_access(&nums);
|
||||
println!("在 nums 中获取随机元素 {}", random_num);
|
||||
println!("\n在 nums 中获取随机元素 {}", random_num);
|
||||
|
||||
/* 长度扩展 */
|
||||
let mut nums = extend(nums, 3);
|
||||
println!("将数组长度扩展至 8 ,得到 nums = {:?}", nums);
|
||||
print!("将数组长度扩展至 8 ,得到 nums = ");
|
||||
inc::print_util::print_array(&arr);
|
||||
|
||||
/* 插入元素 */
|
||||
insert(&mut nums, 6, 3);
|
||||
println!("在索引 3 处插入数字 6 ,得到 nums = {:?}", nums);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
|
||||
/* 删除元素 */
|
||||
remove(&mut nums, 2);
|
||||
println!("删除索引 2 处的元素,得到 nums = {:?}", nums);
|
||||
print!("\n删除索引 2 处的元素,得到 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(&nums);
|
||||
|
||||
/* 查找元素 */
|
||||
let index = find(&nums, 3);
|
||||
println!("在 nums 中查找元素 3 ,得到索引 = {:?}", index);
|
||||
let index = find(&nums, 3).unwrap();
|
||||
println!("\n在 nums 中查找元素 3 ,得到索引 = {}", index);
|
||||
}
|
||||
@@ -1,67 +1,74 @@
|
||||
/**
|
||||
* File: array.rs
|
||||
* File: list.rs
|
||||
* Created Time: 2023-01-18
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
#[allow(unused_variables)]
|
||||
#[allow(unused_variables)]
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化列表 */
|
||||
let mut list: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
println!("列表 list = {:?}", list);
|
||||
|
||||
/* 访问元素 */
|
||||
let num = list[1];
|
||||
println!("访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
println!("将索引 1 处的元素更新为 0 ,得到 list = {:?}", list);
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
println!("清空列表后 list = {:?}", list);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
println!("添加元素后 list = {:?}", list);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
println!("在索引 3 处插入数字 6 ,得到 list = {:?}", list);
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
println!("删除索引 3 处的元素,得到 list = {:?}", list);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
let mut count = 0;
|
||||
for _ in 0..list.len() {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for _ in &list {
|
||||
count += 1;
|
||||
} // 或者
|
||||
// list.iter().for_each(|_| count += 1);
|
||||
// let count = list.iter().fold(0, |count, _| count + 1);
|
||||
|
||||
/* 拼接两个列表 */
|
||||
let mut list1 = vec![6, 8, 7, 10, 9];
|
||||
list.append(&mut list1); // append(移动) 之后 list1 为空!
|
||||
// list.extend(&list1); // extend(借用) list1 能继续使用
|
||||
|
||||
println!("将列表 list1 拼接到 list 之后,得到 list = {:?}", list);
|
||||
|
||||
/* 排序列表 */
|
||||
list.sort();
|
||||
println!("排序列表后 list = {:?}", list);
|
||||
}
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化列表 */
|
||||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("列表 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 访问元素 */
|
||||
let num = list[1];
|
||||
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
print!("将索引 1 处的元素更新为 0 ,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
print!("\n清空列表后 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
print!("\n添加元素后 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
print!("\n删除索引 3 处的元素,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
let mut count = 0;
|
||||
for _ in 0..list.len() {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for _ in &list {
|
||||
count += 1;
|
||||
} // 或者
|
||||
// list.iter().for_each(|_| count += 1);
|
||||
// let count = list.iter().fold(0, |count, _| count + 1);
|
||||
|
||||
/* 拼接两个列表 */
|
||||
let mut list1 = vec![ 6, 8, 7, 10, 9 ];
|
||||
list.append(&mut list1); // append(移动) 之后 list1 为空!
|
||||
// list.extend(&list1); // extend(借用) list1 能继续使用
|
||||
print!("\n将列表 list1 拼接到 list 之后,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
|
||||
/* 排序列表 */
|
||||
list.sort();
|
||||
print!("\n排序列表后 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
}
|
||||
Reference in New Issue
Block a user