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

分析:https://segmentfault.com/a/1190000003794677

哈希表法

复杂度

时间 O(N) 空间 O(N)

思路

整数部分很好处理,只要注意正负号的区分就行了,但是如何处理小数部分呢。如果只是简单的除法,那我们每次把余数乘以10,再除以被除数就可以得到当前位的小数了,得到新的余数,直到余数为0。难点在于,对于无尽循环小数,我们一直这么做永远也不能让余数变为0。这里我们可以用一个哈希表记录每次的余数,如果余数出现重复的时候,说明就产生循环了。为了能找出小数中循环的部分,我们在用哈希表时,还要把每个余数对应的小数位记录下来,这样子我们一旦遇到重复,就知道是从哪里开始循环的。

注意

如果输入的被除数很大,那么余数乘以10有可能溢出,所以我们用long来保存numerator和denominator。

 public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
long num = numerator, den = denominator;
if (num == || den == ) return "";
boolean negative = (num > && den < ) || (num < && den > );
num = Math.abs(num);
den = Math.abs(den);
String integ = (negative ? "-" : "") + String.valueOf(num / den);
num = num % den;
if (num != ) {
HashMap<Long, Integer> map = new HashMap<>();
int pos = ;
StringBuilder frac = new StringBuilder();
while (num != ) {
map.put(num, pos);
num = num * ;
frac.append(num / den);
num = num % den;
if (map.containsKey(num)) {
// 将非循环部分和循环部分分开
String pre = frac.substring(, map.get(num));
String loop = frac.substring(map.get(num));
return integ + "." + pre + "(" + loop + ")";
}
pos++;
}
return integ + "." + frac.toString();
}
return integ;
}
}

Fraction to Recurring Decimal的更多相关文章

  1. 【leetcode】Fraction to Recurring Decimal

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

  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解题报告—— 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 ...

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

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

  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#116]Fraction to Recurring Decimal

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

  9. [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal

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

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

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

随机推荐

  1. python版本升级及pip部署方法

    Python版本升级 CentOS 6.3自带的Python版本为2.6,首先需要升级到2.7版本.由于旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装. 1.下载Pyt ...

  2. [IOC]Unity使用

    Unity是什么? unity是patterns&practices团队开发的一个轻量级.可扩展的依赖注入容器. Unity特性 1.它提供了创建(或者装配)对象实例的机制,而这些对象实例可能 ...

  3. jquery左右链接类似frameset的插件

    一般在使用jquery的扩展的 ui方面的插件, 都要注意链接两个文件 一个是ui 相关的js文件 另一个是 ui相关的 css文件! 参考这个css布局 通常多个平行的items 都是用ul li来 ...

  4. Linux下vim查看文件名

    在vim下编辑时,有时候看不到文件名,不知道编辑的是那个文件,怎么呢,可以按照下面的方法试试. 查看文件名 在正常模式下: :f 或CTRL+G 查看文件的路径 用 :!pwd 可以看当前的详细路径. ...

  5. ls按时间排序输出文件列表

    文件转自:http://www.2cto.com/os/201303/197829.html ls按时间排序输出文件列表   首先,ls --help查看ls相关的与时间排序相关的参数:   > ...

  6. openssh for windows安装

     openssh for windows安装 2009-11-22 22:43:58 分类: WINDOWS 本文转自:http://blog.chinaunix.net/uid-7541208-id ...

  7. 让ie浏览器支持html5新标签的解决方法(使用html5shiv)

    没估计错的话旧版浏览器都是不识别这些新增的标签所以都是用行内元素来处理解决的,所以,有一个解决办法的突破口就是让它变成块状元素就不会处于同一行了,这样在新旧浏览器都是可以显示同样的效果,再者就是让浏览 ...

  8. nyoj 252 01串 动态规划( java)

    当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...

  9. 大数据之tachyon(未完版)

    1.内存文件存储系统 Tachyon是一个开源分布式存储系统,拥有高性能.高容错等优点.并具有类Java的文件API.插件式的底层文件系统.兼容Hadoop MapReduce和 Apache Spa ...

  10. java系列-使用maven创建web项目(二)

    推荐2个maven找jar包配置的网站,只需要搜索关键字即可找到需要的Jar包,非常方便,比如:MySQL就可以找到mysql-connect-Java.jar. http://search.mave ...