Skip to content

Commit c419c72

Browse files
committed
feat: add rust solution to lc problem: No.0166
1 parent e6b0375 commit c419c72

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

solution/0100-0199/0166.Fraction to Recurring Decimal/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,53 @@ function fractionToDecimal(numerator: number, denominator: number): string {
262262
}
263263
```
264264

265+
#### Rust
266+
267+
```rust
268+
use std::collections::HashMap;
269+
270+
impl Solution {
271+
pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String {
272+
if numerator == 0 {
273+
return "0".to_string();
274+
}
275+
let mut ans = String::new();
276+
277+
let neg = (numerator > 0) ^ (denominator > 0);
278+
if neg {
279+
ans.push('-');
280+
}
281+
282+
let mut a = (numerator as i64).abs();
283+
let b = (denominator as i64).abs();
284+
285+
ans.push_str(&(a / b).to_string());
286+
a %= b;
287+
288+
if a == 0 {
289+
return ans;
290+
}
291+
292+
ans.push('.');
293+
294+
let mut d: HashMap<i64, usize> = HashMap::new();
295+
while a != 0 {
296+
if let Some(&pos) = d.get(&a) {
297+
ans.insert(pos, '(');
298+
ans.push(')');
299+
break;
300+
}
301+
d.insert(a, ans.len());
302+
a *= 10;
303+
ans.push_str(&(a / b).to_string());
304+
a %= b;
305+
}
306+
307+
ans
308+
}
309+
}
310+
```
311+
265312
#### C#
266313

267314
```cs

solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,53 @@ function fractionToDecimal(numerator: number, denominator: number): string {
260260
}
261261
```
262262

263+
#### Rust
264+
265+
```rust
266+
use std::collections::HashMap;
267+
268+
impl Solution {
269+
pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String {
270+
if numerator == 0 {
271+
return "0".to_string();
272+
}
273+
let mut ans = String::new();
274+
275+
let neg = (numerator > 0) ^ (denominator > 0);
276+
if neg {
277+
ans.push('-');
278+
}
279+
280+
let mut a = (numerator as i64).abs();
281+
let b = (denominator as i64).abs();
282+
283+
ans.push_str(&(a / b).to_string());
284+
a %= b;
285+
286+
if a == 0 {
287+
return ans;
288+
}
289+
290+
ans.push('.');
291+
292+
let mut d: HashMap<i64, usize> = HashMap::new();
293+
while a != 0 {
294+
if let Some(&pos) = d.get(&a) {
295+
ans.insert(pos, '(');
296+
ans.push(')');
297+
break;
298+
}
299+
d.insert(a, ans.len());
300+
a *= 10;
301+
ans.push_str(&(a / b).to_string());
302+
a %= b;
303+
}
304+
305+
ans
306+
}
307+
}
308+
```
309+
263310
#### C#
264311

265312
```cs
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String {
5+
if numerator == 0 {
6+
return "0".to_string();
7+
}
8+
let mut ans = String::new();
9+
10+
let neg = (numerator > 0) ^ (denominator > 0);
11+
if neg {
12+
ans.push('-');
13+
}
14+
15+
let mut a = (numerator as i64).abs();
16+
let b = (denominator as i64).abs();
17+
18+
ans.push_str(&(a / b).to_string());
19+
a %= b;
20+
21+
if a == 0 {
22+
return ans;
23+
}
24+
25+
ans.push('.');
26+
27+
let mut d: HashMap<i64, usize> = HashMap::new();
28+
while a != 0 {
29+
if let Some(&pos) = d.get(&a) {
30+
ans.insert(pos, '(');
31+
ans.push(')');
32+
break;
33+
}
34+
d.insert(a, ans.len());
35+
a *= 10;
36+
ans.push_str(&(a / b).to_string());
37+
a %= b;
38+
}
39+
40+
ans
41+
}
42+
}

0 commit comments

Comments
 (0)