Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
equal-rational-numbers.py 912 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-06 17:33 +08:00 . Update equal-rational-numbers.py
# Time: O(1)
# Space: O(1)
from fractions import Fraction
class Solution(object):
def isRationalEqual(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
def frac(S):
if '.' not in S:
return Fraction(int(S), 1)
i = S.index('.')
result = Fraction(int(S[:i]), 1)
non_int_part = S[i+1:]
if '(' not in non_int_part:
if non_int_part:
result += Fraction(int(non_int_part), 10**len(non_int_part))
return result
i = non_int_part.index('(')
if i:
result += Fraction(int(non_int_part[:i]), 10**i)
repeat_part = non_int_part[i+1:-1]
result += Fraction(int(repeat_part), 10**i * (10**len(repeat_part)-1))
return result
return frac(S) == frac(T)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助