给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数。
如果小数部分为循环小数,则将重复部分括在括号内。
例如,
    给出 分子 = 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. asp.net连接Access数据库实现登陆功能

    这里话就不多说了,直接演示代码. 连接access数据库首先需要配置web.config <appSettings> <add key="AccessConnString& ...

  2. java序员必备的十大技能

    想成为一名出色的Java程序员么?本文将为大家重点介绍程序员必备的十大技能,成就您的梦想.       1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样 ...

  3. Python 中的字节与字节数组

    Python 中的字节与字节数组 - Python - 伯乐在线 http://python.jobbole.com/84839/

  4. 使用Qt发送HTTPS请求

    示例代码: #include "mainwindow.h" #include "ui_mainwindow.h" #include <QNetworkAc ...

  5. spring 相关博客

    Spring中使用Interceptor拦截器 spirng4   中文文档 ssm整合 Spring系列之Spring常用注解总结 Spring框架中context-param与servlet中in ...

  6. Random 类生成随机数

    Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要 ...

  7. delphi 八字排盘源码(post数据以后,又分析数据)

    procedure TForm1.Button14Click(Sender: TObject);var  ls: TStringList;  lstr: string;  lss: TMemorySt ...

  8. link与import区别

    本质上,这两种方式都是为了加载css文件,但还是存在细微的差别. 差别1:老祖宗的差别,link属于XHTML标签,而@import完全是css提供的一种方式. link标签除了可以加载css外,还可 ...

  9. 在U-Boot中添加自定义命令以实现自动下载程序【转】

    本文转载自:https://gaomf.cn/2016/06/26/%E5%9C%A8U-Boot%E4%B8%AD%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9 ...

  10. 阶乘问题(大数阶乘)简单 n! (一个大数与一个小数相乘的算法 、一个大数与一个小数的除法算法 *【模板】 )

    sdut oj 简单n! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 给定一个数n(0 <= n <= 150), ...