1. 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.

Example 1:

Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:

Input: numerator = 2, denominator = 1
Output: "2"

Example 3:

Input: numerator = 2, denominator = 3
Output: "0.(6)"

当余数重复出现时,意味着循环节出现了。因此需要用一个字典pos记录每个余数出现的位置,重复出现的余数对应的两个位置之间的即为循环节

Note: 运行时发现数字会出现溢出的问题,代码中的类型改成了long long int

class Solution {
public:
string fractionToDecimal(long long int numerator, int denominator) {
string res;
if(numerator < 0 && denominator > 0){
numerator *= -1;
res += "-";
}else if(numerator > 0 && denominator < 0){
denominator *= -1;
res += "-";
}
res += to_string(divmod(numerator, denominator));
if(numerator == 0)return res;
res += ".";
map<int, int>mp;
mp[numerator] = res.size();
while(numerator){
numerator *= 10;
int tmp = divmod(numerator, denominator);
res += to_string(tmp);
if(mp.find(numerator) != mp.end()){
res.insert(mp[numerator], "(");
res += ")";
break;
}
mp[numerator] = res.size();
}
return res; }
long long int divmod(long long int &n1, int &n2){
long long int res = n1 / n2;
n1 -= res * n2;
return res;
}
};

【刷题-LeetCode】166 Fraction to Recurring Decimal的更多相关文章

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

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

  2. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

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

  3. Java for LeetCode 166 Fraction to Recurring Decimal

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

  4. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  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

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

  8. 166. Fraction to Recurring Decimal

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

  9. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

随机推荐

  1. java 网络编程基础 InetAddress类;URLDecoder和URLEncoder;URL和URLConnection;多线程下载文件示例

    什么是IPV4,什么是IPV6: IPv4使用32个二进制位在网络上创建单个唯一地址.IPv4地址由四个数字表示,用点分隔.每个数字都是十进制(以10为基底)表示的八位二进制(以2为基底)数字,例如: ...

  2. CentOS7下使用NFS文件共享给Window server 2012

    CentOS7下使用NFS文件共享给Window server 2012 2018年08月24日 23:15:54 疼迅扣扣 阅读数:443  出自https://blog.csdn.net/u013 ...

  3. 网络编程之UDP(3)丢包总结

    读书笔记 from here UDP socket缓冲区满造成的UDP丢包 如果socket缓冲区满了,应用程序没来得及处理在缓冲区中的UDP包,那么后续来的UDP包会被内核丢弃,造成丢包.在sock ...

  4. 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...

  5. Codeforces Round #358 (Div. 2) C. Alyona and the Tree

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. 第五十个知识点:什么是BLS基于对的签名方案?

    第五十个知识点:什么是BLS基于对的签名方案? BLS签名方案使用了椭圆曲线上了Weil对,本质上是一个在曲线上除n划分的双线性形式,使用 \(n^{th}\) 个单位根. 假设我们有一个椭圆曲线\( ...

  7. C++输出控制小数点后位数的方法

    以C++输出小数点两位数为例 setprecision(n)使用方法总结 首先要记住写头文件 #include <iomanip> //不要忘了头文件 以下是控制小数位数的三种写法 //t ...

  8. <数据结构>拓扑排序

    有向无环图 有向无环图(Directed Acycilc Graph, DAG):从任意顶点出发都无法回到自身的有向图. 拓扑排序 定义 任一两个顶点u,v间,如果存在边u->v,则排序后u一定 ...

  9. xpath如何使用正则、xpath定位svg标签、xpath常用集合

    自己用到的xpath都收集下咯!!! 持续更新本页面 xpath查找svg图标 xpath('//*[local-name() = "svg" and @class="_ ...

  10. 抛砖系列之-MySQL中的数据类型JSON

    今天介绍一个MySQL中的数据类型-JSON,相信大家对JSON都不陌生,在日常工作中使用到的频率也很高,话不多说,直接开始. 何谓JSON 看下RFC文档对于JSON的描述 1.基于 JavaScr ...