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. purfer序列题表

    purfer序列是对于带编号(互不相同)的无根树进行编码得到的,对于同样的n个顶点,其有n-2项,有n^(n-2)种,而且每种都合法(如果只要求他是一棵树的话)(可以通过证明翻译过程维持了各部分的树的 ...

  2. 【简单算法】40.Fizz Buzz

    题目: 写一个程序,输出从 到 n 数字的字符串表示. . 如果 n 是3的倍数,输出“Fizz”: . 如果 n 是5的倍数,输出“Buzz”: .如果 n 同时是3和5的倍数,输出 “FizzBu ...

  3. SDWebImage的使用说明

    1. 在需要的地方导入头文件 #import "UIImageView+WebCache.h" webCache:网络缓存,几乎目前所有的浏览器都有一个内置的缓存,它们通常利用客户 ...

  4. bzoj 1122 [POI2008]账本BBB 模拟贪心,单调队列

    [POI2008]账本BBB Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 524  Solved: 251[Submit][Status][Disc ...

  5. 第一章 深入web请求过程

    B/S架构的的好处: 客户端使用统一的浏览器(browser).由于浏览器的统一性,它不需要特殊的配置和网络连接,有效的屏蔽了不同服务提供商提供给用户使用服务的差异性.另外一点是浏览器的交互特性使得用 ...

  6. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  7. vijos 1471 线性DP+贪心

    描述 Orz教主的成员为教主建了一个游乐场,在教主的规划下,游乐场有一排n个弹性无敌的跳跃装置,它们都朝着一个方向,对着一个巨大的湖,当人踩上去装置可以带你去这个方向无限远的地方,享受飞行的乐趣.但是 ...

  8. 【C++对象模型】第六章 执行期语意学

    执行期语意学,即在程序执行时,编译器产生额外的指令调用,确保对象的构造,内存的释放,以及类型转换与临时对象的生成的安全进行. 1.对象的构造和析构 对于类对象的构造,一般在定义之后则开始内部的构造过程 ...

  9. 【BZOJ1272】Gate Of Babylon [Lucas][组合数][逆元]

    Gate Of Babylon Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description Input ...

  10. 【BZOJ】1709: [Usaco2007 Oct]Super Paintball超级弹珠

    [算法]模拟 [题解]O(n^2)预处理横线(y),纵线(x),主对角线(y-x+n),副对角线(x+y). 然后n^2枚举每个点.