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

 
这个题的思路简单,
1.根据两个数得到结果正负,如负数,添加"-"
2.计算小数点前部分,添加到字符串
3.循环计算小数点后部分,并利用hashmap记录每次的除数,如果在hashmap中存在过,说明存在循环小数部分
4.将循环部分括起来
 
第一次写出的答案如下:
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的更多相关文章

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

  2. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  3. 【leetcode】Fraction to Recurring Decimal

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

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

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

  5. 【LeetCode】166. Fraction to Recurring Decimal

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

  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

    原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...

  8. [LeetCode] Fraction to Recurring Decimal 分数转循环小数

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

  9. [LeetCode#116]Fraction to Recurring Decimal

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

随机推荐

  1. 第二部分 条件控制执行语句、循环语句、switch语句、跳转语句和其它语句

    条件控制执行语句: if语句 if....else....语句 循环语句: while语句 do....while语句 for语句 switch语句: 跳转语句: break; continue; r ...

  2. Java 的String类

    String类 1.String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下: String s = “abc”; s ...

  3. Android 创建Library Project(库项目)与引用操作

    由于在开发过程,为了实现未曾了解的某种效果与特定功能,而求助于网上优秀的开源项目,在使用过程中发现引用开源的Library Project(库项目),的确可以解决很多问题,而且也给出了一种思路,好的软 ...

  4. Spring context:component-scan中使用context:include-filter和context:exclude-filter

    Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...

  5. 二维动态规划——Palindrome

    Palindrome Description A palindrome is a symmetrical string, that is, a string read identically from ...

  6. JavaScript基础:创建对象

    先来看两种简单的对象创建方式: 1.Object构造函数方法 var person = new Object(); person.name = "Nicholas"; person ...

  7. [TPYBoard-Micropython教程之1] 运行第一个脚本——点亮LED

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 会python就能做硬件! 一.TPYBoard V102开发板 TPYBoard V102 ...

  8. webpack入门+react环境配置

    小结放在前:这篇文章主要是为下一篇的react提前铺好路,webpack是一个前端资源模块化管理和打包工具,说白了就是方便我们管理自己的常用的一些代码,比如你开发中用到sass以及jade同时用到es ...

  9. cvs上传复制项目

    现在想重用,特别是重用框架. cvs上传新项目:右键—>team—>share project,根据向导,可选在使用项目名为module名. cvs删除项目:直接在cvs服务器目录上删除项 ...

  10. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSInvocation setArgument:atIndex:]: index (3) out of bounds [-1, 2]'

    这是相机调用方法的时候参数错误