Merge pull request #251 from xBLACKICEx/rust-computational_complexity

Rust computational complexity
This commit is contained in:
Yudong Jin 2023-01-19 02:24:52 +08:00 committed by GitHub
commit 702ab485b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 200 additions and 1 deletions

3
codes/rust/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
**/target
**/worst_best_time_complexity/
**/time_complexity/

75
codes/rust/Cargo.lock generated Normal file
View file

@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chapter_computational_complexity"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

5
codes/rust/Cargo.toml Normal file
View file

@ -0,0 +1,5 @@
[workspace]
members = [
"chapter_computational_complexity",
]

View file

@ -0,0 +1,21 @@
[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"

View file

@ -0,0 +1,50 @@
/**
* File: leetcode_two_sum.rs
* Created Time: 2023-01-14
* Author: xBLACICEx (xBLACKICEx@outlook.com)
*/
use std::collections::HashMap;
struct SolutionBruteForce;
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() {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
}
}
}
vec![]
}
}
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)) {
Some(v) => return vec![*v as i32, i as i32],
None => hm.insert(n, i)
};
}
vec![]
}
}
// Driver Code
fn main() {
// ======= Test Case =======
let nums = vec![2,7,11,15];
let target = 9;
// 方法一
let res = SolutionBruteForce::two_sum(&nums, target);
println!("方法一 res = {:?}", res);
// 方法二
let res = SolutionHashMap::two_sum(&nums, target);
println!("方法二 res = {:?}", res);
}

View file

@ -1,4 +1,9 @@
#![allow(unused_variables)]
/**
* File: time_complexity.rs
* Created Time: 2023-01-10
* Author: xBLACICEx (xBLACKICEx@outlook.com)
*/
#[allow(unused_variables)]
/* 常数阶 */
fn constant(n: i32) -> i32 {
@ -10,6 +15,7 @@ fn constant(n: i32) -> i32 {
count
}
/* 线性阶 */
fn linear(n: i32) -> i32 {
let mut count = 0;
for _ in 0..n {
@ -92,6 +98,7 @@ fn logarithmic(mut n: i32) -> i32 {
count
}
/* 对数阶(递归实现) */
fn log_recur(n: i32) -> i32 {
if n <= 1 {
return 0;

View file

@ -0,0 +1,38 @@
/**
* File: time_complexity.rs
* Created Time: 2023-01-13
* Author: xBLACICEx (xBLACKICEx@outlook.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() {
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);
}
}