From c419c72b137b043d831c595166ad3aefc1dc8a97 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 24 Sep 2025 06:54:25 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.0166 --- .../README.md | 47 +++++++++++++++++++ .../README_EN.md | 47 +++++++++++++++++++ .../Solution.rs | 42 +++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.rs diff --git a/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md b/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md index bf98e9ad90c86..ac50e36c63997 100644 --- a/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md +++ b/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md @@ -262,6 +262,53 @@ function fractionToDecimal(numerator: number, denominator: number): string { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String { + if numerator == 0 { + return "0".to_string(); + } + let mut ans = String::new(); + + let neg = (numerator > 0) ^ (denominator > 0); + if neg { + ans.push('-'); + } + + let mut a = (numerator as i64).abs(); + let b = (denominator as i64).abs(); + + ans.push_str(&(a / b).to_string()); + a %= b; + + if a == 0 { + return ans; + } + + ans.push('.'); + + let mut d: HashMap = HashMap::new(); + while a != 0 { + if let Some(&pos) = d.get(&a) { + ans.insert(pos, '('); + ans.push(')'); + break; + } + d.insert(a, ans.len()); + a *= 10; + ans.push_str(&(a / b).to_string()); + a %= b; + } + + ans + } +} +``` + #### C# ```cs diff --git a/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md b/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md index acf8ab93d3724..92720f7b792b2 100644 --- a/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md +++ b/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md @@ -260,6 +260,53 @@ function fractionToDecimal(numerator: number, denominator: number): string { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String { + if numerator == 0 { + return "0".to_string(); + } + let mut ans = String::new(); + + let neg = (numerator > 0) ^ (denominator > 0); + if neg { + ans.push('-'); + } + + let mut a = (numerator as i64).abs(); + let b = (denominator as i64).abs(); + + ans.push_str(&(a / b).to_string()); + a %= b; + + if a == 0 { + return ans; + } + + ans.push('.'); + + let mut d: HashMap = HashMap::new(); + while a != 0 { + if let Some(&pos) = d.get(&a) { + ans.insert(pos, '('); + ans.push(')'); + break; + } + d.insert(a, ans.len()); + a *= 10; + ans.push_str(&(a / b).to_string()); + a %= b; + } + + ans + } +} +``` + #### C# ```cs diff --git a/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.rs b/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.rs new file mode 100644 index 0000000000000..f10d6f00b14b7 --- /dev/null +++ b/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.rs @@ -0,0 +1,42 @@ +use std::collections::HashMap; + +impl Solution { + pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String { + if numerator == 0 { + return "0".to_string(); + } + let mut ans = String::new(); + + let neg = (numerator > 0) ^ (denominator > 0); + if neg { + ans.push('-'); + } + + let mut a = (numerator as i64).abs(); + let b = (denominator as i64).abs(); + + ans.push_str(&(a / b).to_string()); + a %= b; + + if a == 0 { + return ans; + } + + ans.push('.'); + + let mut d: HashMap = HashMap::new(); + while a != 0 { + if let Some(&pos) = d.get(&a) { + ans.insert(pos, '('); + ans.push(')'); + break; + } + d.insert(a, ans.len()); + a *= 10; + ans.push_str(&(a / b).to_string()); + a %= b; + } + + ans + } +}