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. Lua 之string库

    标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 )) -- ...

  2. uname是什么?

    uname= unix +name, 是指unix 这个操作系统的 名字, 包括 主机名, 内核版本 架构 平台名称等等

  3. 什么时候使用tab键来对齐代码和代码的风格

    在大括号嵌套语法中, 最好是左右(前后) 大括号单独占一行, 这样的嵌套关系最清晰 也就是说, c/c++的风格是最清晰的. 而java php将 左大括号放在上一行的末尾, 稍次一点. 不过在if ...

  4. 来聊聊apply和call

    今天在群里讨论的时候,看到有人问apply怎么使用,突然想起自己刚接触这个方法的时候,也是一样的摸不着头脑. 记得当时上网看了很多讲解,可实际用的时候还是感觉有些蒙蒙哒orz.... 后来想一想,也许 ...

  5. HTML5+ 学习笔记3 storage.增删改查

    //插入N条数据 function setItemFun( id ) { //循环插入100调数据 var dataNum = new Number(id); for ( var i=0; i< ...

  6. 7 天玩转 ASP.NET MVC — 第 1 天

    0. 前言正如标题「7 天玩儿转 ASP.NET MVC」所言,这是个系列文章,所以将会向大家陆续推出 7 篇.设想一下,一天一篇,你将从一个愉快的周一开始阅读,然后在周末成为一个 ASP.NET M ...

  7. mobile touch事件

    touch.js 众所周知,mobile与pc 前端开发的不同中,有一点就是事件的不同,mobile上有touchstart,touchmove,touchend等,而pc上用最多的应该还是我们的cl ...

  8. Linux运维初级教程(四)shell简介

    查看系统可用的shell命令 cat /etc/shells shell是用于与内核进行交流的工具 管道和重定向(< < > > |) |为管道 标准输入的文件描述符为0,标准 ...

  9. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  10. javascript string 函数集

    JavaScript_String对象说明 string中文为"字符串"的意思,String继承自Object对象,此对象提供字符串的查找操作等函数 JavaScript字符串类型 ...