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

Hint:

No scary math, just apply elementary math knowledge. Still remember how to perform a long division?

这题就是按定义做。

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

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

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

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

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

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if(denominator==)
return "";
if(numerator==)
return "";
string res;
long long num=numerator;
long long den=denominator;
if((num<)^(den<))
{
res+="-";
num=abs(num);
den=abs(den);
}
long long temp=num/den;
res+=to_string(temp);
if (num%den==)
return res;
res+=".";
temp=num%den;
map<long long,int> m;
while(temp!=)
{
if(m.find(temp)!=m.end())
{
res.insert(m[temp], , '('); //insert (iterator p, size_t n, char c);
res += ')';
return res;
}
m[temp] = res.size();
temp*=;
res+=to_string(temp/den);
temp%=den;
}
return res;
}
};

Fraction to Recurring Decimal——数值处理&&哈希表的更多相关文章

  1. 【leetcode】Fraction to Recurring Decimal

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

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

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

  3. 【LeetCode】166. Fraction to Recurring Decimal

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

  4. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  5. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  6. 【刷题-LeetCode】166 Fraction to Recurring Decimal

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

  7. [LeetCode] Fraction to Recurring Decimal 分数转循环小数

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

  8. Fraction to Recurring Decimal

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

  9. LeetCode(166) Fraction to Recurring Decimal

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

随机推荐

  1. 禁用 nouveau 驱动

    安装Nvidia显卡的官方驱动和系统自带的nouveau驱动冲突. 安装网上方法尝试了modprob.d/blacklist.conf里的各种修改,重启以后还是没有成功警用nouveau驱动 最后看见 ...

  2. POJ 3984 BFS

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20665   Accepted: 12100 Descriptio ...

  3. 网页导出excel文件

    response.setContentType("application/vnd.ms-excel"); response.setHeader("content-disp ...

  4. JAVA有关命名规范

    包名:          xxxyyyzzz 全小写 类名/接口名:XxxYyyZzz 所有单词首字母大写,其他小写 方法名:       xxxYyyZzz第一个单词首字母小写,其他单词首字母大写 ...

  5. 【算法日记】2.算法中的大O符号

    大O符号是一种算法复杂度的相对表示方式. 1.大O表示算法的操作数,表示出算法运行的快慢 2.大O表示法指出了最糟糕情况下的运行时间,例如 简单查找的运行时间O(n),意味着在最糟糕的情况下,必须运行 ...

  6. MyISAM和InnoDB的行格式ROW_FORMAT

    MyISAM行存储 MyISAM有3种行存储格式:fixed / dynamic / compressed: 格式 说明 备注   fixed  只有当表不包含变长字段(varchar/varbina ...

  7. 图连通性【tarjan点双连通分量、边双联通分量】【无向图】

    根据 李煜东大牛:图连通性若干拓展问题探讨 ppt学习. 有割点不一定有割边,有割边不一定有割点. 理解low[u]的定义很重要. 1.无向图求割点.点双联通分量: 如果对一条边(x,y),如果low ...

  8. [uva11137]立方数之和·简单dp

    小水题再来一发 给定一个正整数n<=1e4,求将n写成若干个正整数立方和的方法数 典型的多阶段模型 f[i][j]表示当前用到1~i的数,累计和为j的方案数. #include<cstdi ...

  9. 【bzoj3476-懒惰的奶牛】线段树

    题解: 感觉这题和别人的做法不一样...呵呵呵...调了一百年.. 设家坐标为(a,b),对于每个点(x,y),可以转化为|a-x|+|b-y|<=k 对于每个点,它的影响范围是一个菱形(也就是 ...

  10. loj515 「LibreOJ β Round #2」贪心只能过样例

    传送门:https://loj.ac/problem/515 [题解] 容易发现S最大到1000000. 于是我们有一个$O(n^2*S)$的dp做法. 容易发现可以被bitset优化. 于是复杂度就 ...