【LeetCode】166. Fraction to Recurring Decimal
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的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
原题地址: https://oj.leetcode.com/problems/fraction-to-recurring-decimal/ 题目内容: Given two integers repre ...
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
随机推荐
- 数学图形之罗马曲面(RomanSurface)
罗马曲面,像是一个被捏扁的正四面体. 本文将展示罗马曲面的生成算法和切图,使用自己定义语法的脚本代码生成数学图形.相关软件参见:数学图形可视化工具,该软件免费开源.QQ交流群: 367752815 维 ...
- WhyEngine游戏引擎作品合集
从9月份开始写三个月内总共实现了13个游戏,5个屏保程序,5个DEMO程序.如果运行时,报有木马病毒什么的,请相信我,这绝对是杀毒软件的误报,自己写的程序由于没有得到杀毒软件的认证,被报有危险是正常的 ...
- convert-a-number-to-hexadecimal
https://leetcode.com/problems/convert-a-number-to-hexadecimal/ // https://discuss.leetcode.com/topic ...
- 第六章 JVM垃圾收集器(2)
上一章记录了几种常见的垃圾收集器,见<第五章 JVM垃圾收集器(1)> 1.G1 说明: 从上图来看,G1与CMS相比,仅在最后的"筛选回收"部分不同(CMS是并发清除 ...
- 十个WEB开发人员不可不知的HTML5工具
Initializr 这是一个HTML5模板创建工具,帮助你得到持续的最新的HTML5样板文件. XRAY XRAY目前支持Safari, Firefox和IE浏览器,XRAY使用了CSS3的多个酷炫 ...
- eclipse全屏模式
- (转)Unity3D Android手机开发环境配置,可真机发布调试
此方法配置好,在可以在unity直接发布到手机上,并可以实时调试. 1.配置eclipse环境:首先在官网下载安装包:http://developer.android.com/sdk/index.ht ...
- sed 常用的命令
n: 读取一行,执行n,把当前行打印到标准输出,再读取一行,覆盖当前行,然后对模式空间执行一组模式/行为.N:读取一行,执行N,再读取一行,现在模式空间有两行内容,执行一组模式/行为.如下:[root ...
- NYoj-119-士兵杀敌(3)-RMQ算法
士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军统率着N个士兵,士兵分别编号为1~N,南将军常常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...
- SqlServer 之 系统视图
一.了解系统视图 1. 系统视图:从名字上看就知道,就是存放一些sqlserver系统的一些信息. 2. 存在位置: 下面截图看看,从截图中你可以看到,不管是“系统数据库”还是“用户数据库”都是有这些 ...