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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/fraction-to-recurring-decimal/description/

题目描述:

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

If the fractional part is repeating, enclose the repeating part in parentheses.

Example 1:

Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:

Input: numerator = 2, denominator = 1
Output: "2"

Example 3:

Input: numerator = 2, denominator = 3
Output: "0.(6)"

题目大意

计算两个整数的除法,结果可能是小数。如果是循环小数,那么就把循环的部分用括号括起来。

解题方法

复习一下整数除法,用长除式的时候,不断地得到当前除法的商和余数,然后给余数部分补零继续做除法。当我们遇到了一个余数,而且这个余数在前面已经出现过了,那么就是出现了循环了。

所以,我们需要一个dict来保存出现过的余数,以及得出这个余数时候,结果出现的位置。所以再次得到这个余数的时候,就查出来了上次出现了的位置,中间这一段就是循环小数部分。

另外特别注意的是,这个题目支持负数除法,所以最好的方法全部转化为正整数的除法,先判断结果的符号,然后把结果变成正数。

代码如下:

class Solution:
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
d = dict()
div, mod = self.divmod(numerator, denominator)
if mod == 0:
return str(div)
ans = "-" if ((numerator > 0) ^ (denominator > 0)) else ""
div, mod, denominator = abs(div), abs(mod), abs(denominator)
ans += str(div) + "."
d[mod] = len(ans)
while mod:
mod *= 10
div, mod = self.divmod(mod, denominator)
ans += str(div)
if mod in d:
index = d[mod]
ans = ans[:index] + "(" + ans[index:] + ")"
break
else:
d[mod] = len(ans)
return ans def divmod(self, a, b):
q = int(a / b) # I'm using 3.x
r = a - b * q
return (q, r)

上面的代码自定义了divmod是因为python的divmod是向下取整,这样的话对于负数不友好。既然按照上面的思路先转化为正数再算,就可以使用原生的divmod。代码如下:

class Solution:
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
if numerator == 0: return "0"
d = dict()
ans = "-" if ((numerator > 0) ^ (denominator > 0)) else ""
numerator, denominator = abs(numerator), abs(denominator)
div, mod = divmod(numerator, denominator)
if mod == 0: return ans + str(div)
ans += str(div) + "."
d[mod] = len(ans)
while mod:
mod *= 10
div, mod = divmod(mod, denominator)
ans += str(div)
if mod in d:
index = d[mod]
ans = ans[:index] + "(" + ans[index:] + ")"
break
else:
d[mod] = len(ans)
return ans

日期

2018 年 9 月 8 日 ———— 美好的周末,从刷题开始

【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)的更多相关文章

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

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

  2. Java for LeetCode 166 Fraction to Recurring Decimal

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

  3. 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告

    原题地址: https://oj.leetcode.com/problems/fraction-to-recurring-decimal/ 题目内容: Given two integers repre ...

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

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

  5. Leetcode#166 Fraction to Recurring Decimal

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

  6. 【LeetCode】166. Fraction to Recurring Decimal

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

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

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

  8. 【leetcode】Fraction to Recurring Decimal

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

  9. 166. Fraction to Recurring Decimal

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

随机推荐

  1. centos 7的命令变化

    1.service -> systemctl命令 2.ifconfig -> ip 命令 3.netstat -> ss命令 4.route -> ip route命令 5.t ...

  2. 02 eclipse中配置Web项目(含eclipse基本配置和Tomcat的配置)

    eclipse搭建web项目 一.Eclipse基本配置 找到首选项: (一)配置编码 (二)配置字体 (三)配置jdk (四)配置Tomcat 二.Tomcat配置 三.切换视图,检查Tomcat ...

  3. 日常Java 2021/11/13

    Java Applet基础 Applet是一种Java程序.它一般运行在支持Java的Web浏览器内.因为它有完整的Java API支持,所以Applet是一个全功能的Java应用程序.如下所示是独立 ...

  4. 一个神奇的JS混淆,JSFuck!

    JSFuck,整体由6个字符[, ], (, ), !, +组成,但却是可以正常运行的JS代码,JSFuck程序可以在任何Web浏览器或引擎中运行解释JavaScript! 看一段代码,源代码为:do ...

  5. Spark(二)【sc.textfile的分区策略源码分析】

    sparkcontext.textFile()返回的是HadoopRDD! 关于HadoopRDD的官方介绍,使用的是旧版的hadoop api ctrl+F12搜索 HadoopRDD的getPar ...

  6. java_IO总结(一)

    所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后) 对于文件内容的操作 ...

  7. Java 将Word转为OFD

    通常在工作中比较常用到的Microsoft Word是属于国外的文档内容编辑软件,其编译技术均属国外.而OFD是一种我国的自主文档格式,在某些特定行业或企业的文档存储技术上是一种更为安全的选择.下面将 ...

  8. Jenkins性能测试

    目录 一.简介 二.JMeter测试 一.简介 Taurus是-个开源的自动化框架,用于运行各种开源负载测试工具和功能测试工具.其支持最流行的开源负载测试工具Apache JMeter.Seleniu ...

  9. [BUUCTF]REVERSE——xor

    xor 附件 步骤: 附件很小,直接用ida打开,根据检索得到的字符串,找到程序关键函数 程序很简单,一开始让我们输入一个长度为33的字符串给v6,然后v6从第二个字符开始与前一个字符做异或运算,得到 ...

  10. uwsgi nginx与django之间的关系以及各自的作用

    首先要明确几个概念及其作用(注意大小写的区别): WSGI uWSGI uwsgi Nginx WSGI 是一种协议,不是任何包不是任何服务器,就和 TCP 协议一样.它定义了 Web 服务器和 We ...