-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path166.py
45 lines (44 loc) · 1.7 KB
/
166.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
__________________________________________________________________________________________________
sample 24 ms submission
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
ans = "-" if numerator*denominator<0 else ""
numerator = abs(numerator)
denominator = abs(denominator)
q, r = numerator//denominator, numerator%denominator
ans += str(q)
if r!=0:
ans += "."
spos = len(ans)
digits = dict()
stat = False
while r!=0:
r *= 10
q, r = r//denominator, r%denominator
if (q,r) in digits:
stat = True
break
digits[q,r] = spos
spos += 1
ans += str(q)
if stat:
ans = ans[:digits[q,r]] + "(" + ans[digits[q,r]:] + ")"
return ans
__________________________________________________________________________________________________
sample 13148 kb submission
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
sign = '-' if numerator*denominator < 0 else ''
nume, deno = abs(numerator), abs(denominator)
n, remainder = divmod(nume, deno)
result = [sign+str(n), '.']
stack = []
while remainder not in stack:
stack.append(remainder)
n, remainder = divmod(remainder*10, deno)
result.append(str(n))
idx = stack.index(remainder)
result.insert(idx+2, '(')
result.append(')')
return ''.join(result).replace('(0)', '').rstrip('.')
__________________________________________________________________________________________________