题目:
题解:
class Solution:def fractionToDecimal(self, numerator: int, denominator: int) -> str:if numerator % denominator == 0:return str(numerator // denominator)s = []if (numerator < 0) != (denominator < 0):s.append('-')# 整数部分numerator = abs(numerator)denominator = abs(denominator)integerPart = numerator // denominators.append(str(integerPart))s.append('.')# 小数部分indexMap = {}remainder = numerator % denominatorwhile remainder and remainder not in indexMap:indexMap[remainder] = len(s)remainder *= 10s.append(str(remainder // denominator))remainder %= denominatorif remainder: # 有循环节insertIndex = indexMap[remainder]s.insert(insertIndex, '(')s.append(')')return ''.join(s)