Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions solution/0100-0199/0166.Fraction to Recurring Decimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64, usize> = 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
Expand Down
47 changes: 47 additions & 0 deletions solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64, usize> = 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
Expand Down
42 changes: 42 additions & 0 deletions solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.rs
Original file line number Diff line number Diff line change
@@ -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<i64, usize> = 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
}
}