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)".

题意很简单,就是把分数转换成小数。

方法也没什么难的,就是利用HashMap来存储余数出现的位置,遇到相同余数,那么就是循环小数了。

需要注意的点:1、int的最小值,这个神烦,只能用long来存储计算。

       2、正负号

       3、整除没有小数点。

public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0){
return "0";
}
long num1 = Math.abs((long)numerator);
long num2 = Math.abs((long)denominator);
StringBuffer str = new StringBuffer();
if (numerator < 0 ^ denominator < 0 ){
str.append('-');
}
long div = num1 / num2;
long mod = num1 % num2;
if (mod == 0){
str.append(div);
return str.toString();
}
Map map = new HashMap<Long,Integer>();
str.append(div+".");
map.put(mod,str.length());
num1 = mod * 10;
while (mod != 0){
div = num1 / num2;
mod = num1 % num2;
str.append(div);
if (map.containsKey(mod)){
str.insert((int)map.get(mod),'(');
str.append(')');
return str.toString();
}
map.put(mod,str.length());
num1 = mod * 10;
}
return str.toString();
}
}

✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java的更多相关文章

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

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

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

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

  3. Java for LeetCode 166 Fraction to Recurring Decimal

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

  4. 166 Fraction to Recurring Decimal 分数到小数

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

  5. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

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

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

  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】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

随机推荐

  1. 连接mysql提示com.mchange.v2.resourcepool.BasicResourcePool

    1.com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@6ff9129c com.mchange.v2.resourc ...

  2. osg渲染数据高程文件

    使用gdal解析DEM文件,将高程数据转换为HeightField对象,然后在osg渲染. 1 源代码 #include <gdal_priv.h> #include <osgVie ...

  3. 和Java相关的一些好文章(不定期更新)

    1.Java 集合类详解 (包括arraylist,linkedlist,vector,stack,hashmap,hashtable,treemap,collection等). 2.Java 理论与 ...

  4. 程序设计入门—Java语言 第五周编程题 2井字棋(5分)

    2 井字棋(5分) 题目内容: 嗯,就是视频里说的那个井字棋.视频里说了它的基本思路,现在,需要你把它全部实现出来啦. 你的程序先要读入一个整数n,范围是[3,100],这表示井字棋棋盘的边长.比如n ...

  5. RMAN的实战篇--备份脚本

    案列一. 目标: 1.每天夜间1 点执行:2.数据库全备,同时备份控制文件及归档日志文件,备份文件保存至: /backup\目录下,并在完成归档日志文件备份后,自动删除已备份的归档日志:3.备份保留7 ...

  6. MySQL临时表

    当你创建临时表的时候,你可以使用temporary关键字.如: create temporary table tmp_table(name varchar(10) not null,passwd ch ...

  7. laravel 中 与前端的一些事5 之解决缓存问题:version

    Version的主要目的就是解决浏览器的缓存问题,在这个方面,Elixir给出的解决方案很完美 应用场景:当我们的css或者js重新更新了,我们需要告诉浏览器我们不要缓存的css或js静态文件样式时, ...

  8. SPSS数据分析—非线性回归

    线性回归的首要满足条件是因变量与自变量之间呈线性关系,之后的拟合算法也是基于此,但是如果碰到因变量与自变量呈非线性关系的话,就需要使用非线性回归进行分析. SPSS中的非线性回归有两个过程可以调用,一 ...

  9. 工作中使用的html5和css3 新特性

    1.placeholder <input type="text" placeholder="请输入手机号码" class="phone" ...

  10. Java环境环境配置

    1. 下载并安装JDK,假如安装在D盘 2. 右键计算机->属性->高级系统设置->环境变量 3. 点击新建系统变量:系统变量名:JAVA_HOME   系统变量值:D:\Progr ...