mycode   73.92%

如何判断同号? 1)res = "-" if ((numerator>0) ^ (denominator>0)) else ""   2)如下

class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
if denominator == 0: return None
if numerator == 0 :return ''
positive = '' if (numerator< 0) is (denominator < 0) else '-'
numerator = abs(numerator) if numerator < 0 else numerator
denominator = abs(denominator) if denominator < 0 else denominator
ans = ''
res = numerator // denominator
add = numerator % denominator
ans += str(res)
if add == 0:
return positive+ans
ans += '.'
repeat = []
while True:
res = add*10 // denominator
add = add*10 % denominator
if (res,add) in repeat:
pos = repeat.index((res,add))
for i,item in enumerate(repeat):
if i==pos:
ans += '('
ans += str(item[0])
ans += ')'
return positive+ans
else:
repeat.append((res,add))
if add == 0:
for i,item in enumerate(repeat):
ans += str(item[0])
return positive+ans

参考:

思路; 我记录了每次的商和余数,其实没必要啦,只需要记录余数即可,因为除数是不变的,余数重复出现了,自然商也就重复啦

class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
""" def div(d,p):
x = d//p
y = d - (x*p )
return (x,y)
if numerator == 0: return ""
res = "-" if ((numerator>0) ^ (denominator>0)) else ""
numerator,denominator = abs(numerator),abs(denominator)
x ,y = div(numerator,denominator)
if y == 0:
return res + str(x)
x,y,denominator = abs(x),abs(y),abs(denominator) res += str(x) + "."
dic = {}
dic[y] = len(res)
while y:
y *= 10
x ,y = div(y , denominator)
res += str(x)
if y in dic:
pos = dic[y]
res = res[:pos] + '(' + res[pos:]+')'
break
else:
dic[y] = len(res)
return res

leetcode-mid- math-166. Fraction to Recurring Decimal的更多相关文章

  1. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  2. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  3. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  4. 【刷题-LeetCode】166 Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  5. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  6. Java for LeetCode 166 Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  7. 166. Fraction to Recurring Decimal (Math)

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  8. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  9. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...

  10. 166 Fraction to Recurring Decimal 分数到小数

    给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".  ...

随机推荐

  1. win10删除文件夹时需要管理员授权或拒绝访问(无权访问权限修改)

    win10 用户:我自己就是电脑主人,凭啥我没有自己电脑文件夹的权限? 微软:对不起,您是电脑硬件的主人,但是电脑系统的主人是我!你只不过是个用户而已. win10 用户:我cao你...[哔-] 对 ...

  2. luogu P4654 [CEOI2017]Mousetrap

    传送门 这里把终点设为根方便后续处理,那么目标就是要让老鼠走到根 首先考虑老鼠动不了的情况,这种情况下可以把从这个点到终点路径上的分支堵住,然后再疏通路径上的走过的边,可以发现这是这种情况下最优的决策 ...

  3. Struts2.5以上版本There is no Action mapped for namespace [/] and action name [userAction_login] associated with context path []

    分析:Struts2在2.5版本后添加strict-method-invocation(严格方法访问),默认为true,不能使用动态方法调用功能,故需设为false struts.xml设置如下: & ...

  4. 查看 php 编译参数

    /app/php/bin/php -i|grep configure 范例 4: [root@VM-001 ~]# /app/php/bin/php -i|grep configure Configu ...

  5. VUE点击颜色选中

  6. DTM/DEM/DSM/DOM/DLG

    一.DTM (Digital Terrain Model) 数字地面模型是利用一个任意坐标系中大量选择的已知x .y .z 的坐标点对连续地面的一个简单的统计表示,或者说,DTM 就是地形表面形态属性 ...

  7. Python核心技术与实战——十八|Python并发编程之Asyncio

    我们在上一章学习了Python并发编程的一种实现方法——多线程.今天,我们趁热打铁,看看Python并发编程的另一种实现方式——Asyncio.和前面协程的那章不太一样,这节课我们更加注重原理的理解. ...

  8. 流式布局和viewport

    流式布局 百分比布局,非固定宽度,内容向两边填充,流动的布局. viewport(视口) PC端的网页在手机端的浏览器显示是不会出现网页的,这是因为移动端的网页不是直接放在移动端的浏览器中,而是放在移 ...

  9. ZROI 19.08.01 生成函数方法

    写在前面:由于我数学基础不好,加上缺乏生成函数知识,所以这一下午我都处在掉线和非掉线的叠加态.而且我写\(\LaTeX\)很慢,所以笔记相当混乱而且不全面.说白了就是我太菜了听不懂. 1.一般生成函数 ...

  10. rsync+inotify实时数据同步单目录实战

    rsync+inotify实时数据同步单目录实战   rsync+inotify实时数据同步单目录实战 inotify是一个强大的.细粒度的.异步的文件系统事件监控机制,linux内核从2.6.13起 ...