标签:
class Solution: # @param {integer} numerator # @param {integer} denominator # @return {string} def fractionToDecimal(self, numerator, denominator): negativeFlag = numerator * denominator < 0 numerator = abs(numerator); denominator = abs(denominator) numsList = [];count = 0; dictLoop = {}; loopstr = None while True: numsList.append(str(numerator / denominator)) numerator = 10 * (numerator%denominator) if numerator == 0: break count += 1 loc= dictLoop.get(numerator) if loc: loopstr = ''.join(numsList[loc:count]) break dictLoop[numerator] = count result = numsList[0] if len(numsList) > 1: result += '.' if loopstr: result += ''.join(numsList[1:len(numsList)-len(loopstr)]) + '(' + loopstr + ')' else: result += ''.join(numsList[1:]) if negativeFlag: result = '-' + result return result
版权声明:本文为博主原创文章,未经博主允许不得转载。
Leetcode 166 Fraction to Recurring Decimal
标签:
原文地址:http://blog.csdn.net/andrew9tech/article/details/46755207