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. Servlet进阶API

    对于每个Servlet的设置信息,web容器会为其生成一个ServletConfig作为代表对象,可以从该对象取得Servlet初始参数,以及代表整个web应用程序的ServletContext对象. ...

  2. easelJS - Cache_vday

    easelJS - Cache_vday $(function() { init(); }); // Cache_vday var canvas; var stage; var container; ...

  3. Java线程: 线程调度

    线程调度是Java多线程的核心,只有好的调度,才能充分发挥系统的性能,提高程序的执行效率. 一.休眠 休眠的目的是使线程让出CPU的最简单做法,线程休眠的时候,会将CPU交给其他线程,以便轮换执行,休 ...

  4. NamingException with message: Name [spring.liveBeansView.mbeanDomain]

    spring mvc启动出现 NamingException with message: Name [spring.liveBeansView.mbeanDomain],解决方式: 在web.xml中 ...

  5. datatable 使用详细说明

    要注意的是,要被dataTable处理的table对象,必须有thead与tbody,而且,结构要规整(数据不一定要完整),这样才能正确处理.以下是在进行dataTable绑定处理时候可以附加的参数: ...

  6. java配置环境变量-及原因

    为什么java要配置环境变量? 那就要从java的编译和解析过程说起 java文件的编译和解析过程 一.一个hellow.java文件,要经历先编译(变成hellow.class),再解析(解析成机器 ...

  7. CodeForces 327C

    Magic Five Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit  ...

  8. Salesforce使用truncate清空数据库

    如果想快速的清空MySQL中的表,可以使用Truncate命令.Truncate能够快速的,对数据进行无差别的清空. 在Mysql中使用truncate的语法是TRUNCATE TABLE [Tabl ...

  9. tableView的编辑

    首先记住声明编辑样式的属性  UITableViewCellEditingStyle 和四个步骤 第一步:让tableView处于编辑状态 [self.rootView.tabView setEdit ...

  10. GCD 多线程 ---的记录 iOS

    先写一个GCD static UserInfoVoModel *userInfoShare = nil; +(instancetype)shareUserInfoVoModel { static di ...