class Solution:
# @param {integer} numerator
# @param {integer} denominator
# @return {string}
def fractionToDecimal(self, numerator, denominator):
result=''
hashmap=dict()
if numerator ==0:
return '0'
if (numerator < 0 ) ^ (denominator < 0):
result += '-'
numerator=abs(numerator)
denominator=abs(denominator)
result += str(numerator/denominator)
r=numerator % denominator
if r == 0:
return result
else:
result+='.'
while r !=0:
idx= hashmap.get(r)
if idx:
mystr=''.join(result[:idx])
mystr+='('
mystr+=''.join(result[idx:])
mystr+=')'
result=mystr
break
hashmap[r]=len(result)
r *= 10
result+=str(r /denominator )
r=r % denominator
return result
Fraction to Recurring Decimal
最新推荐文章于 2024-08-30 10:38:03 发布