给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数。
如果小数部分为循环小数,则将重复部分括在括号内。
例如,
    给出 分子 = 1, 分母 = 2,返回 "0.5".
    给出 分子 = 2, 分母 = 1,返回 "2".
    给出 分子 = 2, 分母 = 3,返回 "0.(6)".

详见:https://leetcode.com/problems/fraction-to-recurring-decimal/description/

Java实现:

class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if(numerator == 0){
return "0";
}
if(denominator == 0){
return "";
} String res = ""; //判断结果是否为负数,加负号
if((numerator<0) ^ (denominator<0)){
res += "-";
} //保证分子分母都为正数,防止取绝对值溢出,先将int转换为long,再取绝对值
long num = numerator;
long den = denominator;
num = Math.abs(num);
den = Math.abs(den); //得到结果的整数部分
long numInt = num/den;
res += String.valueOf(numInt); //判断是否能整除,如果能,则直接返回结果
long number = (num%den)*10;
if(number==0){
return res;
} //结果的小数部分:使用map记录每次操作之后的余数和对应的下标,HashMap的<key, value>分别对应<当前余数, 对应结果的下标>,当出现重复值的时候,可以通过对应下标寻找到重复部分。
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
res += ".";
while(number!=0){
//判断map中是否出现过该余数,如果出现过则开始循环
if(map.containsKey(number)){
int beg = map.get(number); //循环体开始的位置
String part1 = res.substring(0, beg);
String part2 = res.substring(beg, res.length());
res = part1 + "(" + part2 + ")";
return res;
} //继续下除
map.put(number, res.length());
numInt = number / den;
res += String.valueOf(numInt);
number = (number%den) * 10;
} return res;
}
}

详见:https://www.cnblogs.com/grandyang/p/4238577.html

166 Fraction to Recurring Decimal 分数到小数的更多相关文章

  1. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

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

  2. Leetcode166. Fraction to Recurring Decimal分数到小数

    给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...

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

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

  4. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

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

  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 分数转循环小数

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

  8. Java for LeetCode 166 Fraction to Recurring Decimal

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

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

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

随机推荐

  1. 三分钟教你学Git(十四) 之 线下传输仓库

    有时候还有一个人不能从远程直接clone仓库或者说由于非常大,clone非常慢或其他原因.我们能够使用bundle命令将Git仓库打包,然后通过U盘或者是其他介质拷贝给他,这样他拿到打包好的仓库后能够 ...

  2. ORA-01031: insufficient privileges 解决办法

    sysdba不能远程登录这个也是一个很常见的问题了. 碰到这样的问题我们该如何解决呢? 我们用sysdba登录的时候,用来管理我们的数据库实例,特别是有时候,服务器不再本台机器,这个就更是有必要了. ...

  3. oracle问题系列 : ORA-02290: 违反检查约束条件

    报错如下: ### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-022 ...

  4. 序列化FastReport,重要提示少走弯路 good

    原本在开发一个报表插件,因为需要远程传输,因此需要序列化报表,序列化FastReport有两种方式, 1.仅序列化数据,由客户端接受到数据,并呈现报表,这种方式需要在客户端存储报表格式文件xxx.Fr ...

  5. poj2761静态区间第k大

    例题:poj2761 题目要求:给定一个长度为n的序列,给定m个询问,每次询问求[l,r]区间内的第k大: 对于这道题目来说,很多算法都可以使用,比如说树套树(一个负责划分区间,一个负责维护这段区间内 ...

  6. vue 移动端开发

    1.vue开发中的路由: 关于require 与import 的区别 2.vue中的mock数据 3.

  7. UVA11624 Fire! —— BFS

    题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...

  8. 织梦dedecms页面中增加二维码功能的实现方法

    本文介绍了在dedecms中增加二维码功能的实现方法,有时需要在dedecms页面增加二维码,方便手机用户访问,有需要的朋友参考下. 本节内容: dedecms中增加二维码功能   1.打开/incl ...

  9. os、sys和shutil模块

    运行环境:python3 OS模块:os 模块提供了一个统一的操作系统的接口函数 下面的path指路径的意思 os.stat(file) #查询文件属性操作 os.sep #取代操作系统特定的路径分隔 ...

  10. SPFA 最短路 带负权边的---- 粗了解

    SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 算法大致流程是用一个队列来进行维护. 初始时将源加入队列 ...