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. Deadlock_synchromized-Java_se

    class Test implements Runnable{ private boolean flag; Test(boolean flag) { this.flag = flag; } publi ...

  2. Git 设置 用户名 和 邮箱

    git config --global user.name "Vincent" git config --global user.email "********@qq.c ...

  3. libev个人问题解惑

    我们的游戏后端一直以来用的都是libev,之前尝试过去读源码,因为里面用了大量宏和自己也不够耐心的原因,一直没有看懂.这次终于痛下决心,一定要啃下它,于是在这个星期调整自己的工作学习方式(在读源码的过 ...

  4. Codeforces Global Round 4 Prime Graph CodeForces - 1178D (构造,结论)

    Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wan ...

  5. Codeforces Gym 100814C Connecting Graph 树剖并查集/LCA并查集

    初始的时候有一个只有n个点的图(n <= 1e5), 现在进行m( m <= 1e5 )次操作 每次操作要么添加一条无向边, 要么询问之前结点u和v最早在哪一次操作的时候连通了 /* * ...

  6. python中字符串格式化的两种方法

    知识点汇总;1-字符串格式化输出方法一: % 1-print('名字是 %s,年龄是%s' % (name ,age)) 2- %s ---字符串-----相当于执行了str() 3- (name , ...

  7. SpringMVC 使用Servlet原生API作为参数

    具体看代码: @RequestMapping("/testServletAPI") public void testServletAPI(HttpServletRequest re ...

  8. Linux系统无法启动故障解决方案

    Linux系统无法启动故障解决方案 2011-09-27 09:42 佚名 比特网 我要评论(0) 字号:T | T 不管你多么喜爱你的Linux系统机器,有时候你都必须恢复你的系统.是的,即使一台L ...

  9. conda Pyhon版本切换

    1.首先确保你的系统里已经安装了Conda,打开命令行窗口,执行命令:conda --version 2.查看你的系统当前已有的Python环境,执行命令:conda info --envs,从图中我 ...

  10. Linux 安装Samba服务器

    1. 服务器 安装软件: yum -y install samba 创建共享目录并更改目录权限: mkdir -p /home/lee/samba chmod -R 0777 /home/lee/sa ...