166. Fraction to Recurring Decimal (Math)
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)".
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
//deal with overflow (eg: -2147483648/-1, -1/-2147483648)
long l_numerator = (long) numerator;
long l_denominator = (long) denominator;
//deal with negative
string ret = "";
int p = ; //pointer to the return string
if((l_numerator < && l_denominator > ) || (l_numerator > && l_denominator < )) {
ret += "-";
p++;
}
l_numerator = abs(l_numerator);
l_denominator = abs(l_denominator);
long quotient = l_numerator/l_denominator;
long residue = l_numerator%l_denominator;
unordered_map<int,int> res_pos_map;
stringstream ss;
string sTmp;
ss << quotient;
ss >> sTmp;
ret += sTmp;
p+=sTmp.size();
if(residue!=) {
ret += ".";
p++;
res_pos_map[residue]=p;
}
while(residue!=){
l_numerator = residue * ;
quotient = l_numerator/l_denominator;
residue = l_numerator%l_denominator;
ss.clear();
ss << quotient;
ss >> sTmp;
ret += sTmp;
p++;
if(res_pos_map.find(residue)==res_pos_map.end()){
res_pos_map[residue]=p;
}
else{
ss.clear();
ss << quotient;
ss >> sTmp;
ss.clear();
ss << residue*/l_denominator;
ss >> sTmp;
ret = ret.substr(,res_pos_map[residue]) + "(" + ret.substr(res_pos_map[residue]) + ")";
return ret;
}
}
return ret;
}
};
166. Fraction to Recurring Decimal (Math)的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166 Fraction to Recurring Decimal 分数到小数
给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如, 给出 分子 = 1, 分母 = 2,返回 "0.5". ...
随机推荐
- Linux 分支那么多,这里可以帮你缩小选择范围
Linux 分支那么多,这里可以帮你缩小选择范围 https://wiki.installgentoo.com/wiki/Babbies_First_Linux https://wiki.instal ...
- session token两种登陆方式
Session 和 Token 其实Session和Token总体上还是很相似的,但是也有以下区别: 1. 过期时间:Session的过期时间存在cookie的Max-age字段,Token的过期时间 ...
- @Transactional 可以写在 Controller 方法上面了
上图 t1 掉用的service 没定义事物环境,但是 在 t1 上面定义了. 依旧可以 在 参数是5 的 时候 ,让 前面的操作级联回滚. 但是 我不建议这么用,除非特殊需求,正常来说事物根据 ...
- icomoon:生成字体图标的方法并应用
字体图标任意缩放不会失真,也大大减少请求数量,非常好用. 在线生成工具:https://icomoon.io/app/#/select 在线SVG图库(阿里), 用于导入:http://www.ic ...
- requests库(爬虫)
北京理工大学嵩天老师的课程:http://www.icourse163.org/course/BIT-1001870001 官方文档:http://docs.python-requests.org/e ...
- 将SQL for xml path('')中转义的字符正常显示
在工作中出现的发送邮件的时候:因为邮件内容中有链接,并且多个拼接在一起的,于是用了for xml path(). 但是,这样显示出来的链接时会将路径中的<,>,&符号转 ...
- Python撰写mail
版本1 指定邮箱进行发送 """ 说明:指定账户密码进行邮件发送 由312051952@qq.com-->c4kaichen@163.com "&qu ...
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- 并发之volatile底层原理
15.深入分析Volatile的实现原理 14.java多线程编程底层原理剖析以及volatile原理 13.Java中Volatile底层原理与应用 12.Java多线程-java.util.con ...
- linux git clone 指定分支
git clone -b develop http://192.168.11.11:8888/scm/git/vrmmo 指定下载develop分支