原题链接在这里: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. Repeater控件中的三目运算

    <asp:Repeater ID="rptimg" runat="server">        <ItemTemplate>      ...

  2. JBPM4.4+SSH 整合配置及完整实例

    整合jBPM4.4+ssh过程(spring接管struts2和hibernate,例中都整合在application.xml中,没有单独的jbpm.hibernate.cfg.xml): 1.在se ...

  3. <META http-equiv=X-UA-Compatible content=IE=EmulateIE7>

    未来兼容性中的 META 标记和锁定 注意:本文档是预备文档,随时可能变更. 对于 Web 开发人员来说,文本兼容性是一个要考虑的重要问题.Windows Internet Explorer 8 引入 ...

  4. [转]在Entity Framework中使用LINQ语句分页

    本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...

  5. dragloader.js帮助你在页面原生滚动下实现Pull Request操作

    dragloader.js是一个面向移动Web开发的JavaScript库,帮助开发者在使用页面原生滚动时,模拟上/下拉手势,实现Pull Request操作. 在移动设备上,一般会使用 drag d ...

  6. PHP程序员函数注释规格(麻烦大家遵守)

    PHP程序员函数注释规格(麻烦大家遵守)   以前我也不愿意写注释,但是2个月后发现自己写的什么都不知道了.. 为了宇宙的发展,为了二次开发的便捷,为了代码的可读性,建议大家把注释写好.. <? ...

  7. 让Storm插上CEP的翅膀 - Siddhi调研和集成

    什么是 Siddhi? Siddhi 是一种 lightweight, easy-to-use, open source CEP(Complex Event Processing)引擎,由wso2公司 ...

  8. 一 mybatis快速入门

    什么是MyBatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBatis可以使用简 ...

  9. svn 入门

    SVN版本:1.5 及更新版本 名词说明: WC:Working Copy 你的工作区 Versioned:受控的:受版本控制的 SVN是什么? SVN是开源的版本控制系统. 比CVS更多的特性.一个 ...

  10. 【No.3 Ionic】超级逗表情 App

    本人使用Ionic框架开发了一个 超级逗表情 的App 依赖插件 cordova-plugin-app-version 0.1.9 "AppVersion" cordova-plu ...