题目:

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)".

链接: http://leetcode.com/problems/fraction-to-recurring-decimal/

题解:

这道题基本就是Divide Two Integers + 处理循环小数。一刷的时候果断放弃了,因为不想处理恶心的边界条件,不在一开始把int转为long的话后面会很难写。现在又看了一遍大家的答案,发现绝大多数是先把int转为long,我也就放心了。实在不行咱可以用Python写这个。

Java:

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) return "0";
if (denominator == 0) throw new ArithmeticException();
boolean sameSign = (numerator > 0) ^ (denominator < 0);
long dividend = Math.abs((long)numerator);
long divisor = Math.abs((long)denominator); String intPart = String.valueOf(dividend / divisor);
intPart = sameSign ? intPart : "-" + intPart;
long remainder = dividend % divisor;
if (remainder == 0) return intPart; Map<Long, Integer> map = new HashMap<>();
StringBuilder decimalPart = new StringBuilder(); while (remainder != 0) {
map.put(remainder, decimalPart.length());
remainder *= 10;
decimalPart.append(remainder / divisor);
remainder = remainder % divisor;
if (map.containsKey(remainder)) {
decimalPart.insert(map.get(remainder), "(");
decimalPart.append(')');
break;
}
} return intPart + "." + decimalPart.toString();
}
}

二刷:

一样的方法,一样的code,写起来还是很吃力,多多练习吧。

Java:

public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) return "0";
if (denominator == 0) throw new ArithmeticException("Divide by zero");
boolean sameSign = (numerator > 0) ^ (denominator < 0);
long dividend = Math.abs((long)numerator);
long divisor = Math.abs((long)denominator);
StringBuilder sb = new StringBuilder();
if (!sameSign) sb.append("-");
sb.append(dividend / divisor);
dividend %= divisor;
if (dividend == 0) return sb.toString();
sb.append(".");
Map<Long, Integer> map = new HashMap<>(); while (dividend != 0) {
map.put(dividend, sb.length());
dividend *= 10;
sb.append(dividend / divisor);
dividend %= divisor;
if (map.containsKey(dividend)) {
sb.insert(map.get(dividend), "(");
sb.append(")");
return sb.toString();
}
} return sb.toString();
}
}

Test Cases:

  1. (1, 3)
  2. (1, 5)
  3. (1, 6)
  4. (1, 90)
  5. (1, 99)
  6. (22, 7)
  7. (-50, 8)
  8. (0, -5)
  9. (-1, -2147483648)
  10. (-2147483648, 1)

Reference:

https://leetcode.com/discuss/18731/accepted-cpp-solution-with-explainations

https://leetcode.com/discuss/18769/there-good-deal-with-extreme-edge-case-without-converting-long

https://leetcode.com/discuss/18989/online-judge-pass-java-version

https://leetcode.com/discuss/20515/my-java-solution

https://leetcode.com/discuss/22652/do-not-use-python-as-cpp-heres-a-short-version-python-code

https://leetcode.com/discuss/23079/my-clean-java-solution

https://leetcode.com/discuss/31521/short-java-solution

https://leetcode.com/discuss/42159/0ms-c-solution-with-detailed-explanations

https://leetcode.com/discuss/50512/accepted-clean-java-solution

http://gqqnbig.me/?p=160

http://blog.csdn.net/hanshileiai/article/details/8861376

http://segmentfault.com/q/1010000003958185

https://leetcode.com/discuss/8886/my-simple-solution

http://blog.csdn.net/ljiabin/article/details/42025037

166. Fraction to Recurring Decimal的更多相关文章

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

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

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

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

  3. 【LeetCode】166. 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

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

  5. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

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

  6. Java for LeetCode 166 Fraction to Recurring Decimal

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

  7. 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

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

  8. 166. Fraction to Recurring Decimal (Math)

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

  9. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

随机推荐

  1. 20160526-20160531mybatis入门进阶

    mybatis第二天  高级映射 查询缓存 和spring整合 课程复习: mybatis是什么? mybatis是一人持久层框架,mybatis是一个不完全的ORM框架.sql语句需要程序员自己去编 ...

  2. 为 Date 对象添加 ago 属性

    /* * 此处,__defineGetter__与__defineSetter__相当于C#实体中类中的get与set **/ Date.prototype.__defineGetter__('ago ...

  3. [xUnit]尝试单元测试

    参考: http://xunit.github.io/ http://shouldly.readthedocs.org/en/latest/ 创建测试项目工程,类型选择类库,在NuGet中搜索xuni ...

  4. MySQL的记录长度

    MySQL的记录长度 MySQL默认规定一条记录最大的长度是65535字节,所有的字段加在一起所占的字节数不能超过65535.但是MySQL中字段的长度有的时使用字节来规定int,有些字段类型是使用字 ...

  5. [GeekBand] C++学习笔记(2)——BigThree、OOP

    本篇笔记主要分为三个部分,第一部分是以String类为例的基于对象的编程,重点在于构造与析构.拷贝构造函数.拷贝赋值函数三个重要函数.这一部分与笔记(1)中的内容结合起来就是基于对象编程的主要内容.第 ...

  6. 九度OJ 城际公路网 -- 图论

    题目地址:http://ac.jobdu.com/problem.php?pid=1343 题目描述: 为了加快城市之间的通行和物资流动速度,A国政府决定在其境内的N个大中型城市之间,增加修建K条公路 ...

  7. 轻松解决fedora21装完NVIDIA显卡驱动后无法进入gnome问题

    本来打算昨天写的,最近感冒了,打点滴,耽搁了! 我用的是联想14寸笔记本,装好了fedora21后,想装个NVIDIA显卡驱动试试,结果和很多人一样无法进入gnome界面,搞了三四个小时终于搞定.下面 ...

  8. VPN client on linux debian

    Install the pptp-linux and pptp-linux-client: sudo apt-get install pptp-linux pptp-linux-client Crea ...

  9. Apose 套打

    给web添加一个dll引用:Apose.Words 下载链接:http://yunpan.cn/cA7v6uceM6KVw  提取码 11df 在Global.asax里面的Application_S ...

  10. WPF MVVM 中怎样在ViewModel总打开的对话框在窗体之前

    今天在WPF的项目中,写打印插件,在ViewModel中对需要弹出打印对话框,而对话框如果没有Owner所属的时候经常会被当前应用程序遮住,导致我都不知道到底弹出来没有! 参照:http://www. ...