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 ...
随机推荐
- App Store Review Guideline(带翻译)
1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound b ...
- 如何开发基于Dubbo RPC的分布式服务?
什么是Dubbo? Dubbo能做什么? 在Crystal框架下,如何开发基于Dubbo RPC的服务? 在Crystal框架下,如何调用Dubbo RPC服务? 相关的文章 什么是Dubbo? Du ...
- 维护网站时碰到的Office组件导入、导出出现的问题
问题一:检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误: 80070005 解决方法: 1.运行dco ...
- Xcode版本太低引发的bug,xcode各种版本下载方式详解
问题描述: mac系统10.9.5 .之前用的xcode 是5.1.接sdk时,一直报错,编译不过去.最后发现原因是xcode版本太低导致的. 于是去网上找xcode历史版本下载.因为系统版本原因,我 ...
- canvas绘图详解-06-绘制一个五角星-常用绘图原理
先将如何画一个正规的五角星 在五角星的内外画两个圆,五角星有五个角,360/5=72度 所以得出这两个角的度数 然后算出这两个点坐标 角度转弧度 角度/180*Math.PI 所以外顶点坐标 x: ...
- Oracle VS DB2 数据类型
=========================Oracle VS DB2==================================== 本文转自:http://www.bitscn.co ...
- sed 命令详解
sed 用于筛选和转换文本的流编辑器 描述: sed是一个流编辑器,流编辑器对一个输入流执行基本的文本转换(输入流来自文件或者管道行).虽然在某些方面类似于很多可运行脚本的编辑器,但是sed的工作方式 ...
- 使用LVS实现负载均衡原理及安装配置详解
负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学 ...
- 谨慎能捕千秋蝉(三)——界面操作劫持与HTML5安全
一.界面操作劫持 1)ClickJacking ClickJacking点击劫持,这是一种视觉上的欺骗. 攻击者使用一个透明的.不可见的iframe,覆盖在网页的某个位置上,诱使用户点击iframe. ...
- Fragment 学习笔记(1)
网上关于Fragment相关的博客资料很多,写关于这个知识笔记是加深记忆,大神略过: 0x01 了解Fragment 当然看官方文档(http://www.android-doc.com/refere ...