-
Notifications
You must be signed in to change notification settings - Fork 43
/
fraction-to-recurring-decimal.py
76 lines (65 loc) · 2.02 KB
/
fraction-to-recurring-decimal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# V1
# V2
# https://blog.csdn.net/qian2729/article/details/50638161
class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
negativeSign = numerator * denominator < 0
numerator = abs(numerator)
denominator = abs(denominator)
numslist = []
loopDict = {}
cnt = 0
loopStr = None
while True:
numslist.append(str(numerator / denominator))
cnt += 1
numerator = 10 * (numerator % denominator)
if numerator == 0:
break
loc = loopDict.get(numerator)
if loc:
loopStr = ''.join(numslist[loc:cnt])
break
loopDict[numerator] = cnt
ans = numslist[0]
if len(numslist) > 1:
ans += '.'
if loopStr:
ans += ''.join(numslist[1:len(numslist) - len(loopStr)]) + '(' + ''.join(numslist[len(numslist) - len(loopStr):]) + ')'
else:
ans += ''.join(numslist[1:])
if negativeSign:
ans = '-' + ans
return ans
# V3
# Time: O(logn), where logn is the length of result strings
# Space: O(1)
class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
result = ""
if (numerator > 0 and denominator < 0) or (numerator < 0 and denominator > 0):
result = "-"
dvd, dvs = abs(numerator), abs(denominator)
result += str(dvd / dvs)
dvd %= dvs
if dvd > 0:
result += "."
lookup = {}
while dvd and dvd not in lookup:
lookup[dvd] = len(result)
dvd *= 10
result += str(dvd / dvs)
dvd %= dvs
if dvd in lookup:
result = result[:lookup[dvd]] + "(" + result[lookup[dvd]:] + ")"
return result