diff --git a/codes/rust/.gitignore b/codes/rust/.gitignore new file mode 100644 index 000000000..106fe28c3 --- /dev/null +++ b/codes/rust/.gitignore @@ -0,0 +1,3 @@ +**/target +**/worst_best_time_complexity/ +**/time_complexity/ \ No newline at end of file diff --git a/codes/rust/Cargo.lock b/codes/rust/Cargo.lock new file mode 100644 index 000000000..ae2b69a86 --- /dev/null +++ b/codes/rust/Cargo.lock @@ -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" diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml new file mode 100644 index 000000000..91b54bc3b --- /dev/null +++ b/codes/rust/Cargo.toml @@ -0,0 +1,5 @@ + +[workspace] +members = [ + "chapter_computational_complexity", +] \ No newline at end of file diff --git a/codes/rust/chapter_computational_complexity/Cargo.toml b/codes/rust/chapter_computational_complexity/Cargo.toml new file mode 100644 index 000000000..acfb2170c --- /dev/null +++ b/codes/rust/chapter_computational_complexity/Cargo.toml @@ -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" diff --git a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs new file mode 100644 index 000000000..821d52c92 --- /dev/null +++ b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs @@ -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, target: i32) -> Vec { + 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, target: i32) -> Vec { + 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); +} \ No newline at end of file diff --git a/codes/rust/chapter_computational_complexity/time_complexity.rs b/codes/rust/chapter_computational_complexity/time_complexity.rs index a6aa60b31..6ddacc5c7 100644 --- a/codes/rust/chapter_computational_complexity/time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/time_complexity.rs @@ -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; diff --git a/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs b/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs new file mode 100644 index 000000000..a5a979c58 --- /dev/null +++ b/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs @@ -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 { + // 生成数组 nums = { 1, 2, 3, ..., n } + let mut nums = (1..n + 1).collect::>(); + // 随机打乱数组元素 + nums.shuffle(&mut thread_rng()); + nums +} + +/* 查找数组 nums 中数字 1 所在索引 */ +fn find_one(nums: &[i32]) -> Option { + 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); + } +} \ No newline at end of file