mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +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,21 +0,0 @@
|
||||
[package]
|
||||
name = "chapter_computational_complexity"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "leetcode_two_sum"
|
||||
path = "leetcode_two_sum.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "time_complexity"
|
||||
path = "time_complexity.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "worst_best_time_complexity"
|
||||
path = "worst_best_time_complexity.rs"
|
||||
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* File: leetcode_two_sum.rs
|
||||
* Created Time: 2023-01-14
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -11,8 +11,10 @@ struct SolutionHashMap;
|
||||
/* 方法一:暴力枚举 */
|
||||
impl SolutionBruteForce {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
for i in 0..nums.len() - 1 {
|
||||
for j in i + 1..nums.len() {
|
||||
let size = nums.len();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for i in 0..size - 1 {
|
||||
for j in i + 1..size {
|
||||
if nums[i] + nums[j] == target {
|
||||
return vec![i as i32, j as i32];
|
||||
}
|
||||
@@ -25,28 +27,31 @@ impl SolutionBruteForce {
|
||||
/* 方法二:辅助哈希表 */
|
||||
impl SolutionHashMap {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
let mut hm = HashMap::new();
|
||||
|
||||
for (i, n) in nums.iter().enumerate() {
|
||||
match hm.get(&(target - n)) {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
let mut dic = HashMap::new();
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
match dic.get(&(target - num)) {
|
||||
Some(v) => return vec![*v as i32, i as i32],
|
||||
None => hm.insert(n, i)
|
||||
None => dic.insert(num, i as i32)
|
||||
};
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// ======= Test Case =======
|
||||
let nums = vec![2,7,11,15];
|
||||
let nums = vec![ 2, 7, 11, 15 ];
|
||||
let target = 9;
|
||||
|
||||
// 方法一
|
||||
let res = SolutionBruteForce::two_sum(&nums, target);
|
||||
println!("方法一 res = {:?}", res);
|
||||
print!("方法一 res = ");
|
||||
inc::print_util::print_array(&res);
|
||||
// 方法二
|
||||
let res = SolutionHashMap::two_sum(&nums, target);
|
||||
println!("方法二 res = {:?}", res);
|
||||
print!("\n方法二 res = ");
|
||||
inc::print_util::print_array(&res);
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
/**
|
||||
* File: time_complexity.rs
|
||||
* Created Time: 2023-01-10
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
#[allow(unused_variables)]
|
||||
|
||||
/* 常数阶 */
|
||||
fn constant(n: i32) -> i32 {
|
||||
_ = n;
|
||||
let mut count = 0;
|
||||
let size = 100000;
|
||||
let size = 100_000;
|
||||
for _ in 0..size {
|
||||
count += 1
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
@@ -34,6 +34,7 @@ fn array_traversal(nums: &[i32]) -> i32 {
|
||||
count
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
fn quadratic(n: i32) -> i32 {
|
||||
let mut count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
@@ -88,30 +89,30 @@ fn exp_recur(n: i32) -> i32 {
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
fn logarithmic(mut n: i32) -> i32 {
|
||||
fn logarithmic(mut n: f32) -> i32 {
|
||||
let mut count = 0;
|
||||
|
||||
while n > 1 {
|
||||
n = n / 2;
|
||||
while n > 1.0 {
|
||||
n = n / 2.0;
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
fn log_recur(n: i32) -> i32 {
|
||||
if n <= 1 {
|
||||
fn log_recur(n: f32) -> i32 {
|
||||
if n <= 1.0 {
|
||||
return 0;
|
||||
}
|
||||
log_recur(n / 2) + 1
|
||||
log_recur(n / 2.0) + 1
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
fn linear_log_recur(n: f64) -> i32 {
|
||||
fn linear_log_recur(n: f32) -> i32 {
|
||||
if n <= 1.0 {
|
||||
return 1;
|
||||
}
|
||||
let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);
|
||||
let mut count = linear_log_recur(n / 2.0) +
|
||||
linear_log_recur(n / 2.0);
|
||||
for _ in 0 ..n as i32 {
|
||||
count += 1;
|
||||
}
|
||||
@@ -147,7 +148,6 @@ fn main() {
|
||||
|
||||
count = quadratic(n);
|
||||
println!("平方阶的计算操作数量 = {}", count);
|
||||
|
||||
let mut nums = (1..=n).rev().collect::<Vec<_>>(); // [n,n-1,...,2,1]
|
||||
count = bubble_sort(&mut nums);
|
||||
println!("平方阶(冒泡排序)的计算操作数量 = {}", count);
|
||||
@@ -157,12 +157,12 @@ fn main() {
|
||||
count = exp_recur(n);
|
||||
println!("指数阶(递归实现)的计算操作数量 = {}", count);
|
||||
|
||||
count = logarithmic(n);
|
||||
count = logarithmic(n as f32);
|
||||
println!("对数阶(循环实现)的计算操作数量 = {}", count);
|
||||
count = log_recur(n);
|
||||
count = log_recur(n as f32);
|
||||
println!("对数阶(递归实现)的计算操作数量 = {}", count);
|
||||
|
||||
count = linear_log_recur(n.into());
|
||||
count = linear_log_recur(n as f32);
|
||||
println!("线性对数阶(递归实现)的计算操作数量 = {}", count);
|
||||
|
||||
count = factorial_recur(n);
|
||||
|
||||
@@ -1,40 +1,41 @@
|
||||
/**
|
||||
* File: time_complexity.rs
|
||||
* Created Time: 2023-01-13
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
fn random_numbers(n: i32) -> Vec<i32> {
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
let mut nums = (1..n + 1).collect::<Vec<i32>>();
|
||||
// 随机打乱数组元素
|
||||
nums.shuffle(&mut thread_rng());
|
||||
nums
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
fn find_one(nums: &[i32]) -> Option<usize> {
|
||||
for i in 0..nums.len() {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
for _ in 0..10 {
|
||||
let n = 100;
|
||||
let nums = random_numbers(n);
|
||||
let index = find_one(&nums);
|
||||
println!("\n数组 [ 1, 2, ..., n ] 被打乱后 = {:?}", nums);
|
||||
println!("数字 1 的索引为 {:?}", index);
|
||||
}
|
||||
}
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
fn random_numbers(n: i32) -> Vec<i32> {
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
let mut nums = (1..=n).collect::<Vec<i32>>();
|
||||
// 随机打乱数组元素
|
||||
nums.shuffle(&mut thread_rng());
|
||||
nums
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
fn find_one(nums: &[i32]) -> Option<usize> {
|
||||
for i in 0..nums.len() {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
for _ in 0..10 {
|
||||
let n = 100;
|
||||
let nums = random_numbers(n);
|
||||
let index = find_one(&nums).unwrap();
|
||||
print!("\n数组 [ 1, 2, ..., n ] 被打乱后 = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
println!("\n数字 1 的索引为 {}", index);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user