Fraction to Recurring Decimal leetcode
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.
Subscribe to see which companies asked this question
string fractionToDecimal(int numerator, int denominator) {
string ret;
int a = numerator;
int b = denominator;
ret = to_string(a / b);
a = a % b;
if (a)
ret.push_back('.');
else
return ret;
unordered_map<int, int> m;
int i = ret.length() - ;
while (a)
{
i++;
if (m.find(a) != m.end()) {
ret.insert(ret.begin() + m[a], '(');
ret.push_back(')');
break;
}
m[a] = i;
a *= ;
if (a < b) {
ret.push_back('');
continue;
}
ret.push_back('' + a / b);
a = a % b;
}
return ret;
}
提交后发现,有特殊情况没有考虑到。-2147483648 / -1 这种情况会溢出,所以需要使用long类型替换int。
参考修改后代码如下:
string fractionToDecimal(int numerator, int denominator) {
if (!numerator) return "";
string ret;
if (numerator < ^ denominator < ) ret += '-';
long a = numerator < ? (long)numerator * (-) : (long)numerator;
long b = denominator < ? (long)denominator * (-) : (long)denominator;
long c = a / b;
ret += to_string(c);
a = a % b;
if (!a) return ret;
ret.push_back('.');
unordered_map<long, long> m;
int i = ret.length() - ;
while (a)
{
i++;
if (m.find(a) != m.end()) {
ret.insert(ret.begin() + m[a], '(');
ret.push_back(')');
break;
}
m[a] = i;
a *= ;
if (a < b) {
ret.push_back('');
continue;
}
ret.push_back('' + a / b);
a = a % b;
}
return ret;
}
虽然在leetcode上提交成功了,但是在vs2015上运行是错误的,
long a = numerator < 0 ? (long)numerator * (-1) : (long)numerator;
这一句如果是numerator是-2147483648,a会是-2147483648,这非常诡异。不知道vs2015编译器对于这个问题是怎么解决的。
Fraction to Recurring Decimal leetcode的更多相关文章
- 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 ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【leetcode】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 弗洛伊德判环
分数转小数,要求输出循环小数 如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 Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- iOS 英语
allocation:分配 agrregate :聚合,聚集,总计.在iOS中是一种文件类型. atomically:原子级的 archiver:归档.例如,oc专门有归档类NSKeyedArchiv ...
- Java之模板方法模式(Template Method)
Java之模板方法模式(Template Method) 1. 概念:定义一个算法的骨架,而将一些实现步骤延迟到子类中. 把不变的行为搬到超类,去除子类中重复的代码来体现他的优势. 2. UML图: ...
- Flex移动应用程序开发的技巧和窍门(一)
这是一个由多个部分组成的系列文章的第一部分,它包含了Flex移动开发的若干技巧.如果你过去习惯于桌面和Web编程,你会发现,开发移动应用程序将面临一系列新的挑战. 除了重新思考你的对数据存储和处理的策 ...
- Swiper.js使用遇到的问题总结onSlideChangeEnd回调偶尔触发,偶尔不触发等;
Swiper 是一个开源免费的移动触摸插件. 在使用中遇到这样一个问题,记录一下. page 间切换效果 使用 fade 的时候,如果每个页面的大小不一样, 比如第一个页面全屏, 第二个页面比第一个小 ...
- HDU5914
Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- 《HelloGitHub月刊》第10期
前言 这一年感谢大家的支持,小弟这里给大家拜年了! <HelloGitHub月刊>会一直做下去,欢迎大家加入进来提供更多的好的项目. 最后,祝愿大家:鸡年大吉- <HelloGitH ...
- 【.net 深呼吸】项目中是否有必要删去多余的引用
很多大伙伴们常常会苦思一个问题:项目代码中用不到的引用,是不是应该删除,以避免代码在编译后存在太多的无意义引用? 其实,这个问题,你完全可以自己去应证的,咋应证呢?知道反射吗,对了,只要你知道这玩意儿 ...
- Android安卓身份证识别SDK
一.Android安卓身份证识别SDK应用背景 这些年,随着互联网金融的极速发展,第三方支付.理财.P2P网贷.征信等APP应用成爆发式的增长,在众多APP中都涉及到对身份证信息的录入,如第三方支付. ...
- 使用Hugo搭建GitHub个人博客
主题概况 Hugo 是一个用 Go 语言编写的静态网站生成器.类似的静态网站生成器还有Jekyll.hexo等等.以上生成器都使用过,但感觉要么环境麻烦,要么生成静态页面步骤繁琐以及生成缓慢.如果你正 ...
- iis7.0 ExtensionlessUrlHandler-Integrated-4.0解决方法
IIS7.0上部署网站,打开后500错误: 处理程序“ExtensionlessUrlHandler-Integrated-4.0” 在其模块列表中有一个错误模块“ManagedPipelineHan ...