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)".
解题思路:

测试中会出现各种临界值,因此,需要先将 numerator 和 denominator转为long,然后计算出整数部分,对于小数部分,将numerator*10后处理,一方面将计算除后的值放到一个list中,另一方面将新的分子放到map里,由于新分子肯定小于denominator,所以肯定会出现0或者重复(因此分数要么有限,要么是无限循环小数)一旦新分母重复出现,意味着结果会出现循环值,将其按照规则写出来即可,JAVA实现如下:

  1. public String fractionToDecimal(int numerator, int denominator) {
  2. StringBuilder sb = new StringBuilder();
  3. if (numerator < 0 && denominator > 0)
  4. sb.append("-");
  5. else if (numerator > 0 && denominator < 0)
  6. sb.append("-");
  7. Long Lnumerator = (long) numerator;
  8. if (Lnumerator < 0)
  9. Lnumerator = -Lnumerator;
  10. Long Ldenominator = (long) denominator;
  11. if (Ldenominator < 0)
  12. Ldenominator = -Ldenominator;
  13. if (Lnumerator % Ldenominator == 0){
  14. sb.append(Lnumerator / Ldenominator);
  15. return sb.toString();
  16. }
  17. sb.append(Lnumerator / Ldenominator + ".");
  18. System.out.println(Lnumerator);
  19. Lnumerator %= Ldenominator;
  20. System.out.println(Lnumerator);
  21. HashMap<Long, Integer> map = new HashMap<Long, Integer>();
  22. ArrayList<Integer> list = new ArrayList<Integer>();
  23. map.put(Lnumerator, 0);
  24. while (true) {
  25. Lnumerator *= 10;
  26. while (Lnumerator < Ldenominator) {
  27. list.add(0);
  28. map.put(Lnumerator, list.size());
  29. Lnumerator *= 10;
  30. }
  31. list.add((int) (Lnumerator / Ldenominator));
  32. Lnumerator %= Ldenominator;
  33. if (Lnumerator == 0) {
  34. for (int i = 0; i < list.size(); i++)
  35. sb.append(list.get(i));
  36. return sb.toString();
  37. } else if (map.containsKey(Lnumerator)) {
  38. for (int i = 0; i < map.get(Lnumerator); i++)
  39. sb.append(list.get(i));
  40. sb.append("(");
  41. for (int i = map.get(Lnumerator); i < list.size(); i++)
  42. sb.append(list.get(i));
  43. sb.append(")");
  44. return sb.toString();
  45. }
  46. map.put(Lnumerator, list.size());
  47. }
  48. }

Java for LeetCode 166 Fraction to Recurring Decimal的更多相关文章

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

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

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

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

  3. Leetcode#166 Fraction to Recurring Decimal

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

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

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

  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

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

  8. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...

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

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

随机推荐

  1. Solr -- 实时搜索

    在solr中,实时搜索有3种方案 ①soft commit,这其实是近实时搜索,不能完全实时. ②RealTimeGet,这是实时,但只支持根据文档ID的查询. ③和第一种类似,只是触发softcom ...

  2. office 软件常用备忘(删除线/冻结等)

    Word: 1 大纲显示/navigation display: 视图/View --> Navigation Pane (left) 2 Track changes: 显示删除线 / 修改地方 ...

  3. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  4. “面向对象"和"面向过程"到底有什么区别?

    链接:http://www.zhihu.com/question/27468564/answer/101951302 当软件还非常简单的时候,我们只需要面向过程编程: 定义函数函数一函数二函数三函数四 ...

  5. groovy-集合

    Lists 你能使用下面的方法创建一个lists,注意[]是一个空list. 1 def list = [5, 6, 7, 8] 2 assert list.get(2) == 7 3 assert  ...

  6. 在 ASP.NET MVC 3 中应用 KindEditor

    http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html 第一步 将 KindEditor 的源文件添加到项目中,建议放到 /Scr ...

  7. AndroidManifest File Features

    http://www.android-doc.com/guide/topics/manifest/manifest-intro.html The following sections describe ...

  8. MyISAM和InnoDB的索引在实现上的不同

    1 MyISAM只把索引载入内存,数据缓存依赖于操作系统,InnoDB把索引和数据都载入内存缓冲 2 MyISAM数据库中的数据是按照插入的顺序保存,在每个索引节点中保存对应的数据行的地址,理论上说主 ...

  9. HttpClient教程

    2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大.但是如果我们直接使用已 ...

  10. mac 下终端访问文件出现“Permission Denied”解决方案

    mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...