-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0166-FractionToRecurringDecimal.cs
48 lines (41 loc) · 1.46 KB
/
0166-FractionToRecurringDecimal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//-----------------------------------------------------------------------------
// Runtime: 76ms
// Memory Usage: 22.9 MB
// Link: https://leetcode.com/submissions/detail/375095783/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0166_FractionToRecurringDecimal
{
public string FractionToDecimal(int numerator, int denominator)
{
if (numerator == 0) return "0";
StringBuilder sb = new StringBuilder();
if (numerator < 0 ^ denominator < 0) sb.Append("-");
long dividend = Math.Abs((long)numerator);
long divisor = Math.Abs((long)denominator);
sb.Append((dividend / divisor).ToString());
long remainder = dividend % divisor;
if (remainder == 0) return sb.ToString();
sb.Append('.');
var map = new Dictionary<long, int>();
while (remainder != 0)
{
if (map.ContainsKey(remainder))
{
sb.Insert(map[remainder], "(");
sb.Append(")");
break;
}
map[remainder] = sb.Length;
remainder *= 10;
sb.Append((remainder / divisor).ToString());
remainder %= divisor;
}
return sb.ToString();
}
}
}