原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/

题目:

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.

Example 1:

Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:

Input: numerator = 2, denominator = 1
Output: "2"

Example 3:

Input: numerator = 2, denominator = 3
Output: "0.(6)"

题解:

加入整数部分后,在reminder 不为0的时候添加小数部分,用HashMap来维护reminder 和 对应的小数长度. 若是出现无限循环可以找到加左括号的起始点.

AC Java:

 public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if(numerator == 0){
return "0";
}
if(denominator == 0){
return "";
} StringBuilder res = new StringBuilder();
long num = Math.abs((long)numerator);
long deno = Math.abs((long)denominator); //如果是负数
if((numerator < 0) ^ (denominator < 0)){
res.append("-");
} res.append(num/deno);
long reminder = num%deno;
if(reminder == 0){
return res.toString();
} //有小数
res.append(".");
//HashMap中计入reminder 和 对应的小数部分长度,用来找到循环开始的位置
HashMap<Long, Integer> hm = new HashMap<Long, Integer>();
StringBuilder desRes = new StringBuilder();
while(reminder != 0){
if(hm.containsKey(reminder)){ //出现了循环,找到循环开始的index添加左括号
int beginIndex = hm.get(reminder);
desRes.insert(beginIndex, "(");
desRes.append(")");
res.append(desRes);
return res.toString();
}
hm.put(reminder, desRes.length());
desRes.append(reminder*10/deno);
reminder = (reminder*10)%deno;
}
res.append(desRes);
return res.toString();
}
}

Decimal To Fraction 小数转换成分数类似.

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

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

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

  2. [LeetCode] Fraction to Recurring Decimal 哈希表

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

  3. 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 ...

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

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

  5. 【leetcode】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 弗洛伊德判环

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

  7. 【LeetCode】166. Fraction to Recurring Decimal

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

  8. 【刷题-LeetCode】166 Fraction to Recurring Decimal

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

  9. [LeetCode#116]Fraction to Recurring Decimal

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

随机推荐

  1. 去掉Xcode源码末尾的空格

    去掉Xcode源码末尾的空格 在用 Xcode 开发的时候,很容易就在行末增加一些空格了.这些空格在上传到 review board 上后 , 就会被特别的颜色显示出来.因为一种好的编程风格是说 , ...

  2. 离线更新SEPM服务器的病毒定义库

    1. 从http://www.symantec.com/security_response/definitions/download/detail.jsp?gid=sep下载JDB文件    2. 将 ...

  3. POJ 1703 Find them, Catch them(种类并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41463   Accepted: ...

  4. virtual box ubuntu卡在开机光标

    创建虚拟机的时候选择之前保存的虚拟机盘vdi文件,打开的时候卡在光标.原来是因为虚拟机是64位的,但是新建的时候只有32位的ubuntu可以选择就选择的32位. 解决办法: 在bios设置里,打开cp ...

  5. PHP 错误与异常 笔记与总结(10)错误处理器测试

    关联文件:myErrorHandler.php (上一篇) 先测试通知级别的错误的自定义处理: testErrorHandler.php <?php require_once 'myErrorH ...

  6. python 不得不知的第三方库以及常用安装包

    mysql 驱动$ sudo pip install MySQL-python redis 数据库$ sudo pip install redis django 全文搜索$ sudo pip inst ...

  7. PHP 随机显示

    <?php  print_r(    array_rand(      array(        "新春快乐"=>"",        " ...

  8. 20145317彭垚 《Java程序设计》第4周学习总结

    20145317彭垚 <Java程序设计>第04周学习总结 20145317彭垚 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承就是避免多个类间重复定义共同行 ...

  9. 区分super和this

    Java关键字this.super使用总结 一.this Java关键字this只能用于方法方法体内.当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 ...

  10. jquery 选择器,模糊匹配

    按姓名匹配 1,name前缀为aa的所有div的jquery对象 $("div[name^='aa']"); 2,name后缀为aa的所有div的jquery对象 $(" ...