forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine some details and coding style for Rust codes (krahets#344)
* Refine some details and coding style for Rust codes * Update coding style for Rust codes * Update time_complexity.rs * Update array.rs * Update leetcode_two_sum.rs * Update hash_map.rs * Update file headers * Update coding style for Rust codes and Zig codes * Update coding style for Rust codes and Zig codes --------- Co-authored-by: Yudong Jin <[email protected]>
- Loading branch information
1 parent
6dc2169
commit e65c7bd
Showing
20 changed files
with
189 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/** | ||
/* | ||
* File: array.rs | ||
* Created Time: 2023-01-15 | ||
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
use rand::Rng; | ||
|
||
|
@@ -45,17 +47,16 @@ fn remove(nums: &mut Vec<i32>, index: usize) { | |
} | ||
} | ||
|
||
#[allow(unused_variables)] | ||
/* 遍历数组 */ | ||
fn traverse(nums: &[i32]) { | ||
let mut count = 0; | ||
let mut _count = 0; | ||
// 通过索引遍历数组 | ||
for _ in 0..nums.len() { | ||
count += 1; | ||
_count += 1; | ||
} | ||
// 直接遍历数组 | ||
for _ in nums { | ||
count += 1; | ||
_count += 1; | ||
} | ||
} | ||
|
||
|
@@ -73,37 +74,37 @@ fn find(nums: &[i32], target: i32) -> Option<usize> { | |
fn main() { | ||
let arr = [0; 5]; | ||
print!("数组 arr = "); | ||
inc::print_util::print_array(&arr); | ||
print_util::print_array(&arr); | ||
// 在 Rust 中,指定长度时([i32; 5])为数组 | ||
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度 | ||
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组(Array)也是rust一般情况下使用动态数组的类型 | ||
let nums = vec![ 1, 3, 2, 5, 4 ]; | ||
print!("\n数组 nums = "); | ||
inc::print_util::print_array(&nums); | ||
print_util::print_array(&nums); | ||
|
||
/* 随机访问 */ | ||
// 随机访问 | ||
let random_num = random_access(&nums); | ||
println!("\n在 nums 中获取随机元素 {}", random_num); | ||
|
||
/* 长度扩展 */ | ||
// 长度扩展 | ||
let mut nums = extend(nums, 3); | ||
print!("将数组长度扩展至 8 ,得到 nums = "); | ||
inc::print_util::print_array(&arr); | ||
print_util::print_array(&arr); | ||
|
||
/* 插入元素 */ | ||
// 插入元素 | ||
insert(&mut nums, 6, 3); | ||
print!("\n在索引 3 处插入数字 6 ,得到 nums = "); | ||
inc::print_util::print_array(&nums); | ||
print_util::print_array(&nums); | ||
|
||
/* 删除元素 */ | ||
// 删除元素 | ||
remove(&mut nums, 2); | ||
print!("\n删除索引 2 处的元素,得到 nums = "); | ||
inc::print_util::print_array(&nums); | ||
print_util::print_array(&nums); | ||
|
||
/* 遍历数组 */ | ||
// 遍历数组 | ||
traverse(&nums); | ||
|
||
/* 查找元素 */ | ||
// 查找元素 | ||
let index = find(&nums, 3).unwrap(); | ||
println!("\n在 nums 中查找元素 3 ,得到索引 = {}", index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
/** | ||
/* | ||
* File: list.rs | ||
* Created Time: 2023-01-18 | ||
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
|
||
#[allow(unused_variables)] | ||
include!("../include/include.rs"); | ||
|
||
/* Driver Code */ | ||
/* Driver Code */ | ||
fn main() { | ||
/* 初始化列表 */ | ||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ]; | ||
print!("列表 list = "); | ||
inc::print_util::print_array(&list); | ||
// 初始化列表 | ||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ]; | ||
print!("列表 list = "); | ||
print_util::print_array(&list); | ||
|
||
/* 访问元素 */ | ||
let num = list[1]; | ||
println!("\n访问索引 1 处的元素,得到 num = {num}"); | ||
// 访问元素 | ||
let num = list[1]; | ||
println!("\n访问索引 1 处的元素,得到 num = {num}"); | ||
|
||
/* 更新元素 */ | ||
list[1] = 0; | ||
print!("将索引 1 处的元素更新为 0 ,得到 list = "); | ||
inc::print_util::print_array(&list); | ||
// 更新元素 | ||
list[1] = 0; | ||
print!("将索引 1 处的元素更新为 0 ,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
/* 清空列表 */ | ||
list.clear(); | ||
print!("\n清空列表后 list = "); | ||
inc::print_util::print_array(&list); | ||
// 清空列表 | ||
list.clear(); | ||
print!("\n清空列表后 list = "); | ||
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.push(1); | ||
list.push(3); | ||
list.push(2); | ||
list.push(5); | ||
list.push(4); | ||
print!("\n添加元素后 list = "); | ||
print_util::print_array(&list); | ||
|
||
/* 中间插入元素 */ | ||
list.insert(3, 6); | ||
print!("\n在索引 3 处插入数字 6 ,得到 list = "); | ||
inc::print_util::print_array(&list); | ||
// 中间插入元素 | ||
list.insert(3, 6); | ||
print!("\n在索引 3 处插入数字 6 ,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
/* 删除元素 */ | ||
list.remove(3); | ||
print!("\n删除索引 3 处的元素,得到 list = "); | ||
inc::print_util::print_array(&list); | ||
// 删除元素 | ||
list.remove(3); | ||
print!("\n删除索引 3 处的元素,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
/* 通过索引遍历列表 */ | ||
let mut count = 0; | ||
for _ in 0..list.len() { | ||
count += 1; | ||
} | ||
// 通过索引遍历列表 | ||
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); | ||
// 直接遍历列表元素 | ||
_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); | ||
// 拼接两个列表 | ||
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 = "); | ||
print_util::print_array(&list); | ||
|
||
/* 排序列表 */ | ||
list.sort(); | ||
print!("\n排序列表后 list = "); | ||
inc::print_util::print_array(&list); | ||
// 排序列表 | ||
list.sort(); | ||
print!("\n排序列表后 list = "); | ||
print_util::print_array(&list); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/** | ||
/* | ||
* File: leetcode_two_sum.rs | ||
* Created Time: 2023-01-14 | ||
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
use std::collections::HashMap; | ||
struct SolutionBruteForce; | ||
|
@@ -49,9 +51,9 @@ fn main() { | |
// 方法一 | ||
let res = SolutionBruteForce::two_sum(&nums, target); | ||
print!("方法一 res = "); | ||
inc::print_util::print_array(&res); | ||
print_util::print_array(&res); | ||
// 方法二 | ||
let res = SolutionHashMap::two_sum(&nums, target); | ||
print!("\n方法二 res = "); | ||
inc::print_util::print_array(&res); | ||
} | ||
print_util::print_array(&res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/** | ||
/* | ||
* File: time_complexity.rs | ||
* Created Time: 2023-01-10 | ||
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
*/ | ||
|
||
/* 常数阶 */ | ||
fn constant(n: i32) -> i32 { | ||
|
@@ -167,4 +167,4 @@ fn main() { | |
|
||
count = factorial_recur(n); | ||
println!("阶乘阶(递归实现)的计算操作数量 = {}", count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
/** | ||
/* | ||
* File: time_complexity.rs | ||
* Created Time: 2023-01-13 | ||
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
use rand::seq::SliceRandom; | ||
use rand::thread_rng; | ||
|
||
|
@@ -35,7 +37,7 @@ | |
let nums = random_numbers(n); | ||
let index = find_one(&nums).unwrap(); | ||
print!("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); | ||
inc::print_util::print_array(&nums); | ||
print_util::print_array(&nums); | ||
println!("\n数字 1 的索引为 {}", index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/** | ||
/* | ||
* File: hash_map.rs | ||
* Created Time: 2023-02-05 | ||
* Author: sjinzh ([email protected]) | ||
*/ | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
use std::collections::HashMap; | ||
|
||
|
@@ -19,7 +21,7 @@ pub fn main() { | |
map.insert(13276, "小法"); | ||
map.insert(10583, "小鸭"); | ||
println!("\n添加完成后,哈希表为\nKey -> Value"); | ||
inc::print_util::print_hash_map(&map); | ||
print_util::print_hash_map(&map); | ||
|
||
// 查询操作 | ||
// 向哈希表输入键 key ,得到值 value | ||
|
@@ -30,11 +32,11 @@ pub fn main() { | |
// 在哈希表中删除键值对 (key, value) | ||
_ = map.remove(&10583); | ||
println!("\n删除 10583 后,哈希表为\nKey -> Value"); | ||
inc::print_util::print_hash_map(&map); | ||
print_util::print_hash_map(&map); | ||
|
||
// 遍历哈希表 | ||
println!("\n遍历键值对 Key->Value"); | ||
inc::print_util::print_hash_map(&map); | ||
print_util::print_hash_map(&map); | ||
println!("\n单独遍历键 Key"); | ||
for key in map.keys() { | ||
println!("{key}"); | ||
|
@@ -43,4 +45,4 @@ pub fn main() { | |
for value in map.values() { | ||
println!("{value}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/** | ||
/* | ||
* File: binary_search.rs | ||
* Created Time: 2023-02-05 | ||
* Author: sjinzh ([email protected]) | ||
*/ | ||
*/ | ||
|
||
/* 二分查找(双闭区间) */ | ||
fn binary_search(nums: &[i32], target: i32) -> i32 { | ||
|
@@ -49,11 +49,11 @@ pub fn main() { | |
let target = 6; | ||
let nums = [ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 ]; | ||
|
||
/* 二分查找(双闭区间) */ | ||
// 二分查找(双闭区间) | ||
let mut index = binary_search(&nums, target); | ||
println!("目标元素 6 的索引 = {index}"); | ||
|
||
/* 二分查找(左闭右开) */ | ||
// 二分查找(左闭右开) | ||
index = binary_search1(&nums, target); | ||
println!("目标元素 6 的索引 = {index}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/** | ||
/* | ||
* File: bubble_sort.rs | ||
* Created Time: 2023-02-05 | ||
* Author: sjinzh ([email protected]) | ||
*/ | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
/* 冒泡排序 */ | ||
fn bubble_sort(nums: &mut [i32]) { | ||
|
@@ -44,10 +46,10 @@ pub fn main() { | |
let mut nums = [ 4, 1, 3, 1, 5, 2 ]; | ||
bubble_sort(&mut nums); | ||
print!("冒泡排序完成后 nums = "); | ||
inc::print_util::print_array(&nums); | ||
print_util::print_array(&nums); | ||
|
||
let mut nums1 = [ 4, 1, 3, 1, 5, 2 ]; | ||
bubble_sort_with_flag(&mut nums1); | ||
print!("\n冒泡排序完成后 nums1 = "); | ||
inc::print_util::print_array(&nums1); | ||
print_util::print_array(&nums1); | ||
} |
Oops, something went wrong.