Leetcode#166 Fraction to Recurring Decimal
计算循环小数
先把负数转化成正数,然后计算,最后添加符号
当被除数重复出现的时候,说明开始循环了,所以用一个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的更多相关文章
- 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 解题报告(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 ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- Nginx配置:http重定向,URLRewrite,一个简单框架的配置思路
一个重定向的应用配置: server { listen 8000; server_name localhost; root F:/home/projects/test; index ...
- android使用library工程问题
在windows系统下,library project必须和project处于相同的盘符中,因为如果在不同盘符,project.properties中的android.library.referenc ...
- Windows7下Microsoft Office Excel 不能访问文件解决方案
1).开始--〉运行--〉cmd 2)命令提示符下面,输入mmc -32,打开32的控制台 3).文件菜单中,添加删除管理单元--〉组件服务 4).在"DCOM配置"中找到&quo ...
- 百度 迷你版 UMeditor富文本编辑器 使用方法
第一步:下载编辑器 到官网下载 umeditor 最新版源码版本,下载之后打开 _examples/index.html 就可以看到演示例子.[下载页面] 第二步:部署编辑器到页面 解压下载的包,放到 ...
- linux内核SPI总线驱动分析(一)(转)
linux内核SPI总线驱动分析(一)(转) 下面有两个大的模块: 一个是SPI总线驱动的分析 (研究了具体实现的过程) 另一个是SPI总线驱动的编写(不用研究具体的实现过程) ...
- [转]Openwrt的Inittab
转来一篇关于启动的文章,特意收藏.http://see.sl088.com/wiki/Inittab 文件位于/etc/inittab编辑方法vi /etc/inittab初始内容::sysinit: ...
- LaTex中让页码从正文开始编号
在正文和目录之前这样设置即可 \setcounter{page}{}
- sed实例一则
1.背景: test.txt文件里有这些语句 li^E1026^D20150802B07QH800^B698.^C20150801B08CDP00^B514.^C20150803D00A8L00^B2 ...
- hdu 5444 Elven Postman
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...
- iOS学习之Object-C语言内存管理
一.内存管理的方式 1.iOS应用程序出现Crash(闪退),90%的原因是因为内存问题. 2.内存问题: 1)野指针异常:访问没有所有权的内存,如果想要安全的访问,必须 ...