给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数。
如果小数部分为循环小数,则将重复部分括在括号内。
例如,
    给出 分子 = 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. C++ JSON解析库RapidJSON

    https://github.com/Tencent/rapidjson jsontext.txt { "result" : [ { "face_id" : & ...

  2. JVM内存分配策略、各个代区、FullGC/MinorGC

    主要讨论默认的Serial/Serial Old内存分配: 一.几种分配方案 1.  对象优先在Eden分配: 一般情况下,对象会在新生代的Eden区分配,Eden区没有足够空间时,虚拟机会 发起一次 ...

  3. 使用pt-query-digest进行日志分析

    使用pt-query-digest sudo apt install percona-toolkit 也可以到官网 https://www.percona.com/downloads/percona- ...

  4. 2013级新生程序设计基础竞赛-正式赛 F 异或最大值 解题报告

    F - 异或最大值 Time Limit: 2000/1000MS (Java/Others)      Memory Limit: 128000/64000KB (Java/Others) Subm ...

  5. codeforces 667B B. Coat of Anticubism(水题)

    题目链接: B. Coat of Anticubism time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. 并不对劲的bzoj1758:p4292:[WC2010]重建计划

    题目大意 \(n\)(\(n\leq10^5\))个点的一棵树,有边权\(w\),给定\(l,r\),求边数在\([l,r]\)中的路径的平均边权的最大值 题解 二分答案,判断时将边权变成\(w-mi ...

  7. JavaScript-Tool:wdtree

    ylbtech-JavaScript-Tool:wdtree 1.返回顶部 1. 插件描述:wdTree是一个轻量级jQuery插件用于创建一个带有嵌套Check Boxe的树形控件. wdTree是 ...

  8. css原生变量 var

    css 变量的支持情况: 在pc端支持还不错,ie不支持,移动端也不支持,大概这就是css变量没有广泛使用的原因吧 CSS预编译工具Sass/Less/Stylus的便捷之处就是可以定义变量,方便代码 ...

  9. Event Handling Guide for iOS--(三)---Event Delivery: The Responder Chain

    Event Delivery: The Responder Chain 事件传递:响应链 When you design your app, it’s likely that you want to ...

  10. Linux中的gdb调试方法总结