166 Fraction To Recurring Decimal
166 Fraction To Recurring Decimal
Fraction to Recurring Decimal 
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
If multiple answers are possible, return any of them.
It is guaranteed that the length of the answer string is less than 104 for all the given inputs.
Example 1:
1
2
3
4
**Input:** numerator = 1, denominator = 2
**Output:** "0.5"
Example 2:
1
2
3
4
**Input:** numerator = 2, denominator = 1
**Output:** "2"
Example 3:
1
2
3
4
**Input:** numerator = 4, denominator = 333
**Output:** "0.(012)"
Constraints:
1
2
-231 <= numerator, denominator <= 231 - 1
denominator != 0
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
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
if numerator == 0:
return "0"
fraction = []
if (numerator < 0) ^ (denominator < 0):
fraction.append("-")
dividend = abs(numerator)
divisor = abs(denominator)
fraction.append(str(dividend // divisor))
remainder = dividend % divisor
if remainder == 0:
return "".join(fraction)
fraction.append(".")
map_dict = {}
while remainder != 0:
if remainder in map_dict:
fraction.insert(map_dict[remainder], "(")
fraction.append(")")
break
map_dict[remainder] = len(fraction)
remainder *= 10
fraction.append(str(remainder // divisor))
remainder %= divisor
return "".join(fraction)
This post is licensed under CC BY 4.0 by the author.