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实现如下:

    public String fractionToDecimal(int numerator, int denominator) {
StringBuilder sb = new StringBuilder();
if (numerator < 0 && denominator > 0)
sb.append("-");
else if (numerator > 0 && denominator < 0)
sb.append("-");
Long Lnumerator = (long) numerator;
if (Lnumerator < 0)
Lnumerator = -Lnumerator;
Long Ldenominator = (long) denominator;
if (Ldenominator < 0)
Ldenominator = -Ldenominator;
if (Lnumerator % Ldenominator == 0){
sb.append(Lnumerator / Ldenominator);
return sb.toString();
}
sb.append(Lnumerator / Ldenominator + ".");
System.out.println(Lnumerator);
Lnumerator %= Ldenominator;
System.out.println(Lnumerator);
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
map.put(Lnumerator, 0);
while (true) {
Lnumerator *= 10;
while (Lnumerator < Ldenominator) {
list.add(0);
map.put(Lnumerator, list.size());
Lnumerator *= 10;
}
list.add((int) (Lnumerator / Ldenominator));
Lnumerator %= Ldenominator;
if (Lnumerator == 0) {
for (int i = 0; i < list.size(); i++)
sb.append(list.get(i));
return sb.toString();
} else if (map.containsKey(Lnumerator)) {
for (int i = 0; i < map.get(Lnumerator); i++)
sb.append(list.get(i));
sb.append("(");
for (int i = map.get(Lnumerator); i < list.size(); i++)
sb.append(list.get(i));
sb.append(")");
return sb.toString();
}
map.put(Lnumerator, list.size());
}
}

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. Learn sed using these command on Linux(流线式编辑器——sed)

    是对文件中的每一行进行处理,不会对源文件进行修改 sed --version sed '11d' sed_file sed -n '/[Bb]erry/p' sed_file (由于设置了n,所以只打 ...

  2. python 类型之 set

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  3. BZOJ-1192 鬼谷子的钱袋 2^n有关数论

    1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2473 Solved: 1806 [Submit][St ...

  4. BZOJ-1002 轮状病毒 高精度加减+Kirchhoff矩阵数定理+递推

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3543 Solved: 1953 [Submit][Statu ...

  5. CodeMap

    CodeMap 这是在博客园看到的一位朋友文章介绍的,很好用的插件,所有的方法,注释块在右边一目了然,找代码方便极了,还能设置代码段的高亮,给代码段设置标识

  6. [Angularjs]单页应用之分页

    写在前面 在项目中,用到单页应用的分页,当时想到使用滚动加载的方案,可是几次尝试都没配置成功,闲着无聊就弄了一个demo. 系列文章 [Angularjs]ng-select和ng-options [ ...

  7. abstract 类 构造函数

    public abstract class CommonReq { private String TransNo { get; set; } public String SubmitData { ge ...

  8. 慎用 Enum.GetHashCode()

    公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...

  9. emmet使用 及 notepadd++ emmet的安装

    emmet的使用的参考文章:http://www.cnblogs.com/sussski/p/3544744.html html:4s.html:4t.html:5或! +.>.^:层次 *.@ ...

  10. PRML Chapter 2. Probability Distributions

    PRML Chapter 2. Probability Distributions P68 conjugate priors In Bayesian probability theory, if th ...