【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 ...
随机推荐
- Glusterfs初试
Gluster的模式及介绍在此不表,这里只记录安装及配置过程. 1.整体环境 server1 : gfs1.cluster.com server2 : gfs2.cluster.com Client: ...
- csv2txt.cpp
#include <iostream> #include <fstream.h> #include <windows.h> #include <iomanip ...
- Linux下分割、合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具
Linux下分割.合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具 Linux下分割.合并PDF(pdftk) pdftk http://www.pdflabs.com/doc ...
- 如何获取隔壁wifi密码,非暴力破解
目前常见的Wi-Fi加密方式有WEP.WPA2和WPS(链接为各自的破解方式),不过有网友反映以往破解WPA2的方法耗时太长,而且不适用于所有WPS启动的接入点.而今天介绍的这种方法则更加省时省力. ...
- 用Java操纵HBase数据库(新建表,插入,删除,查找)
java代码如下: package db.insert; /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.i ...
- 如何在Windows 7 或Vista中修改MTU
Windows操作系统使用Maximum Transmission Unit (MTU) 来确定在下面的网络层上可以传输的协议数据包(protocol data packet)的最大尺寸. MTU参数 ...
- GO语言基础之error
Go错误处理 Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } ...
- 【Nodejs】cheerio简单示例
cheerio的API挺多,我也了解有限,欲知详情请参考 “通读cheerio API”. 下面就事论事聊聊它的基本使用. 比如说在某网页中有这么一段HTML: </tbody> < ...
- CAD打开慢,卡在99%
问题描述 打开AutoCAD的时候,软件停留在加载99%,点击出现[无法响应],要么等待,要么强行关闭,若平时正常关闭CAD时也异常缓慢. 原因分析 破解版,没有联网就激活了.CAD默认启动需要联网, ...
- core dump相关
linux下生成core dump文件方法及设置 http://www.2cto.com/os/201310/253450.html 在linux平台下,设置core dump文件生成的方法: 1 ...