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. mysql复制表结构,复制表数据

    MYSQL 复制表 show create table table_name:查看表的建表语句.该语句包含了原数据表的结构,索引等. 使用 SHOW CREATE TABLE 命令获取创建数据表(CR ...

  2. React中构造函数constractor,为什么要用super(props)

    前言 昨天晚上公司组织了前端分享会,在讲到React Class方法的时候,有的同学提出,为什么构造函数一定要super,我记得我之前看的黑马视频里面有讲过,就再翻出来 内容 React官方中文文档里 ...

  3. vue项目1-pizza点餐系统3-路由知识点补充

    1.可以通过tag修改router-link的默认标签 <!--router-link标签默认是a标签,tag标签可以修改默认标签 --> <li><router-lin ...

  4. kickstart一键装机部署

    1.第一步安装DHCP yum -y install dhcp 1.1配置修改文件 cat >> /etc/dhcp/dhcpd.conf <<END subnet 172.1 ...

  5. JavaScript中实现li向上轮播

    在网上找了很久,没有找到合适的模板,其实我这个也是公司用的,希望以后也能复用,节省时间 function scrollAuto(scrollBox, list){//两个参数分别填列表的ul的clas ...

  6. 将.py文件转换成.exe文件

    1.安装pyinstaller模块 pip install pyinstaller 2.打开python目录下的scripts文件夹 cmd下,使用pyinstaller -F test.py,运行后 ...

  7. Mockplus、Axure、墨刀软件对比

    Mockplus 优点:基础版免费使用,操作简单,上手快,交互简单(只需拖曳就可以),功能多样,组件资源丰富,预览方式和导出类型多样,支持团队协作. 缺点:不支持手势交互. Axure 优点:操作变化 ...

  8. 怎么画一条0.5px的边

    编者按:本文由人人网FED发表于掘金,并已授权奇舞周刊转载 什么是像素? 像素是屏幕显示最小的单位,在一个1080p的屏幕上,它的像素数量是1920 1080,即横边有1920个像素,而竖边为1080 ...

  9. TCP/IP基础总结性学习(2)

    简单的HTTP协议 一.HTTP 协议用于客户端和服务器端之间的通信 客户端和服务器的定义:请求访问文本或图像等资源的一端称为客户端,而提供资源响应的一 端称为服务器端.在两台计算机之间使用 HTTP ...

  10. 《Python3-标准库》讲解

    一.string:文本常量和模板 函数:capwords()-------------------------------------------------- import string  s = ...