原题地址

计算循环小数

先把负数转化成正数,然后计算,最后添加符号

当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数

需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧,比Hard难度的题还低。

最残暴的做法是直接转成64位长整型,比如下面的代码。好处是代码简洁了许多,不过这是投机取巧,因为如果题目把参数换成两个64为长整型,这个方法就不行了。

如果不用64长整型,就用32位普通整型怎么办?

由于我们要把除数和被除数转化成正数,所以当其中任意一个数等于INT_MIN时,就得考虑溢出问题。

对于被除数,情况还好。假如被除数是INT_MIN,大不了我先减一个除数,让商加1就可以避免这个问题。这个技术可以用在Divide Two Integers这道题中(参见这篇文章

对于除数,情况就复杂了。假如除数是INT_MIN,除数不能拆分,所以没法转换成正数去做。这怎么办?

有人可能会想,用无符号类型呗。的确,32位无符号数的最大表示范围是2^32 - 1 > 2^31,可以将除数转化成正数了。但是仍然会有溢出发生,因为在计算小数部分时,被除数如果小于除数就要不断乘以10,直到够除为止。这个不断乘以10的过程会溢出,因为无符号整型最多只能保存2倍大的除数(INT_MIN),而被除数很容易超过这个值。为了解决这个问题,需要将除法运算退化成减法去做,将乘法运算退化成加法。把乘以10改成加9次,把除以除数改成减若干次除数。显然,这样做太麻烦了。

也有人可能会想,干嘛要转成正数呢?直接在int上做呗,对不起,还会遇到上面所说的溢出的情况。

看来只使用32位数据类型没有办法了,如果非要坚持不用64位整型,也可以,大整数除法等待着你。

代码:

 string fractionToDecimal(int numerator, int denominator) {
if (numerator == )
return ""; long long num = numerator;
long long den = denominator;
map<long long, int> record;
string res = (numerator ^ denominator) < 0 ? "-" : "";
string rem;
int i = ; num = abs(num);
den = abs(den);
res += to_string(num / den);
num = (num % den) * ; while (num) {
if (record.find(num) == record.end())
record[num] = i;
else {
rem = rem.substr(, record[num]) + "(" + rem.substr(record[num]) + ")";
break;
}
rem += to_string(num / den);
num = (num % den) * ;
i++;
}
if (!rem.empty())
res += "." + rem; return res;
}

Leetcode#166 Fraction to Recurring Decimal的更多相关文章

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

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

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

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

  3. Java for LeetCode 166 Fraction to Recurring Decimal

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

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

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

  5. 【LeetCode】166. Fraction to Recurring Decimal

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

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

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

  7. 【leetcode】Fraction to Recurring Decimal

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

  8. 166. Fraction to Recurring Decimal

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

  9. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

随机推荐

  1. Mongodb添加地理位置索引

    1.同步站点信息到mongo中(支持mysql.sqlserver数据同步) 2.在Collections文件夹下所在文档右键,在菜单中选择Add Index, 3.然后进行数据查询{ "m ...

  2. phpQuery采集微信公众号文章乱码

    终于找到解决方案了,这是一个值得庆祝的事情.... 原来是因为微信在源码中加入了防采集代码<!--headTrap<body></body><head>< ...

  3. Linux安全运维日志排查几个 tips

    运维日志排查记录 前言 记录一些排查常见日志的命令,方法wiki,欢迎补充(Markdown 语法). 常用命令 查找关键词并统计行数 cat 2015_7_25_test_access.log | ...

  4. android开源代码

    Android开源项目--分类汇总 转自:https://github.com/Trinea/android-open-project Android开源项目第一篇——个性化控件(View)篇 包括L ...

  5. R语言中判断是否是整数。以及读写excel

    今天接手一个重复性工作, 需要手工把产品运营们在excel里写的活动规则, 插入数据库表中.为了减少出错, 提高效率. 再加上最近刚刚学R语言, 就用R练练手, 自动生成mysql的sql语句. 一次 ...

  6. ios 工程图片清理shell

    #!/bin/shecho "随意删除@2x图片可能会引起错误 因为ios工程会更加前缀和分辨率自己找到@2x的图片 所以删除@2x图片时要慎重"read -n1 -p  &quo ...

  7. Oracle private dblink和pubic dblink

    DB : 11.2.0.3.0 Oracle DBLINK 创建分为private 和 public dblink,默认创建的为private ; private dblink 只有创建的schema ...

  8. 创建自己的oracle解释计划

    1.解释计划 当使用explain plan来为一个查询生成预期的执行计划时,输出将包括一下几种: SQL访问的每一张表: 访问每张表的方法: 每一个需要联结的数据源所使用的联结方法: 按次序列出的所 ...

  9. Objective-C 编码规范

    Objective-C 编码规范,内容来自苹果.谷歌的文档翻译,自己的编码经验和对其它资料的总结. 概要 Objective-C 是一门面向对象的动态编程语言,主要用于编写 iOS 和 Mac 应用程 ...

  10. Objective-C中Block语法、Block使用以及通过Block实现数组排序

    Block:语法块,本质上是匿名函数(没有名称的函数) 标准C里面没有Block,C语言的后期扩展版本,加入了匿名函数 在C++.JS.Swift等语言有类似语法,叫做闭包 Block语法和C语言里的 ...