166 Fraction to Recurring Decimal
166 Fraction to Recurring Decimal
这道题就是不停的去check做除法的余数是否重复出现了
class Solution: # @param {integer} numerator # @param {integer} denominator # @return {string} def fractionToDecimal(self, n, d): isNeg = (n * d < 0) n, d = abs(n), abs(d) ans= '' ans += str(n/d) if isNeg: ans = '-' + ans if n % d == 0: return ans ans += '.' dic = {} n = n % d while True: if n not in dic: dic[n] = len(ans) else: pos = dic[n] return ans[:pos] + "(" + ans[pos:] + ")" n *= 10 if n % d == 0: ans += str(n/d) return ans else: r = n / d n %= d ans += str(r)