Fraction to Recurring Decimal

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.

For example,

  • Given numerator = 1, denominator = 2, return "0.5".
  • Given numerator = 2, denominator = 1, return "2".
  • Given numerator = 2, denominator = 3, return "0.(6)".

Credits:
Special thanks to @Shangrila for adding this problem and creating all test cases.

这题就是按定义做。

如果不能整除,就不断进行余数补零除以除数。

维护一个映射表map<long long, int> m, 用来记录每个除数对应返回值ret中的位置。

(1)当出现重复的除数n时,说明找到了循环体,根据m[n]找到ret中位置,加上相应的'('和')'将循环体括起来即可返回。

(2)当余数r为0时,返回ret。

注意点:

1、正负号

2、分子为0

3、可能出现INT_MIN/-1的越界情况,因此第一步现将int转为long long int

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
// special cases
if(numerator == )
return "";
string ret = "";
// type conversion in case of INT_MIN
long long n = numerator;
long long d = denominator;
// sign
int sign = ;
bool digit = false;
if((n<) ^ (d<))
sign = -;
n = abs(n);
d = abs(d);
unordered_map<long long, int> m; // numerator --> position
while(true)
{
if(n < d)
{
if(digit == false)
{
if(ret == "")
ret = "0.";
else
ret += ".";
digit = true;
}
n *= ;
}
int r = n - n/d*d;
if(r == )
{
ret += to_string(n/d);
if(sign == -)
ret = "-" + ret;
return ret;
}
else
{
if(digit == true)
{// check recurring
if(m.find(n) == m.end())
{
ret += to_string(n/d);
m[n] = ret.size()-;
}
else
{
int pos = m[n];
ret = ret.substr(, pos) + "(" + ret.substr(pos) + ")";
if(sign == -)
ret = "-" + ret;
return ret;
}
}
else
{
ret += to_string(n/d);;
}
n = r;
}
}
}
};

【LeetCode】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

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

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

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

  4. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

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

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

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

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

  7. Java for LeetCode 166 Fraction to Recurring Decimal

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

随机推荐

  1. wifiphisher使用介绍

    1.github地址:https://github.com/sophron/wifiphisher 2.需要安装在kali linux下面 3.需要两个无线网卡 4.安装方法是使用介绍,参考githu ...

  2. spring cloud 报错Error creating bean with name 'hystrixCommandAspect' ,解决方案

    spring cloud 升级到最新版 后,报错: org.springframework.beans.factory.BeanCreationException: Error creating be ...

  3. 20 个具有惊艳效果的 jQuery 图像缩放插件

    jQuery相对与Flash的魔力已经贯穿整个网络.尽管,Flash层被认为是用于网页设计的首选,然而随着jQuery的出现,以及他的酷似Flash的交互式特效使得网页更加的优雅——Flash开始靠边 ...

  4. 理解JavaScript里this关键字

    1.全局代码中的this:始终指向window 2.函数代码中的this: }; var bar = { x: , test: function () { alert(this === bar); a ...

  5. C/C++语言中变量作用域:局部变量,全局变量,文件级变量

    C/C++语言中的变量分为全局变量和局部变量. 这样的划分方式的根据是变量的可见范围或者叫做作用域. 1 局部变量 局部变量指的是定义在{}中的变量,其作用域也在这个范围内.尽管常见的局部变量都是定义 ...

  6. 网络结构设计——负载均衡之LVS学习笔记(二)

    LVS按个人理解的说就是将一台Linux服务器当作路由器等功能的技术.LVS---Linux虚拟服务器. LVS实现了三种IP负载均衡技术VS/NAT.VS/TUN.VS/DR. 今天简单分享一下我在 ...

  7. 县级以上联动js实现无需数据库的行政区域下拉控件

    代码共享url: http://code.google.com/p/region-select-js/ 数据已经更新到中国统计局网站中的2012年的那批数据(制作后未核对); 下拉使用div模拟实现. ...

  8. COM中的几个基本概念

    类厂 组件结构示例 DllGetClassObject COM库与类厂的交互

  9. Mac Finder中如何复制当前完整路径

    1.拖到命令行 2.在Finder中command+i 会弹出详细信息,然后[位置]处进行 copy 3.利用Automator,添加一个服务的快捷键. 转自:http://q.cnblogs.com ...

  10. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...