2025-09-24 18:51:01 +0300 MSK
Fraction to Recurring Decimal
Links
Code
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)