【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 ...
随机推荐
- JAVA常见算法题(三十五)
判断一个整数能被几个9整除. public static void main(String[] args) { f(729); f(730); } public static void f(int n ...
- P值(P-value),“差异具有显著性”和“具有显著差异”
郑冰刚提到P值,说P值的定义(着重号是笔者加的,英文是从WikiPedia摘来的): P值就是当原假设为真时,比所得到的样本观察结果更极端的结果出现的概率. The P-value is the pr ...
- HttpMessageConverter
HttpMessageConverter<T>是Spring3的一个重要接口,它负责将请求信息转换为一个对象(类型为T),将对象(类型为T)输出为响应信息. DispatcherServl ...
- 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSTEM
ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSTEM 表空间 > 编辑 表空间: SYSTEM 1. 一般信息返 ...
- 判断 iframe 是否加载完毕
我能想到的有以下几种方式: 方法一.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(function( ...
- 修改一行SQL代码 性能提升了100倍
在PostgreSQL中修改了一行不明显的代码,把(ANY(ARRAY[...]) 改成 ANY(VALUES(...))),结果查询时间从20s变为0.2s.最初我们学习使用 EXPLAN ANAL ...
- JAVA输出指定目录下的子目录和子文件
题目:给定一个目录,要求输出这个目录下面的子目录和子文件 逻辑: 先判断给定的是不是一个合法的目录,如果不是,则提示给定错误 如果是目录,那么使用File.listFile()获得这个目录下文件名的数 ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
- GO语言基础之并发concurrency
并发Concurrency 很多人都是冲着 Go 大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码的解析来看,goroutine 只是由官方实现的超级“线程池”而已.不过话说回来,每个实例 4-5KB的 ...
- Android -- Gradle
使用gradle的目的 更容易重用资源和代码: 可以更容易创建不同的版本的程序,多个类型的apk包: 更容易配置,扩展; 更好的IDE集成; Gradle基本结构 使用ide创建的gradle构建的项 ...