classSolution(object): deffractionToDecimal(self, numerator, denominator): """ :type numerator: int :type denominator: int :rtype: str """ prefix = '-'if numerator*denominator < 0else'' numerator = abs(numerator) denominator = abs(denominator) divide = numerator//denominator mod = numerator%denominator if mod == 0:return prefix+str(divide) d = {} stack = [] i = -1 whileTrue: if mod == 0: return prefix+str(divide)+'.'+''.join(stack) mod *= 10 i += 1 if mod in d: idx = d[mod] return prefix+str(divide)+'.'+''.join(stack[:idx])+'('+''.join(stack[idx:])+')' if mod < denominator: stack.append('0') d[mod] = i else: stack.append(str(mod//denominator)) d[mod] = i mod = mod%denominator