✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
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的更多相关文章
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166 Fraction to Recurring Decimal 分数到小数
给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如, 给出 分子 = 1, 分母 = 2,返回 "0.5". ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
随机推荐
- 深入浅出设计模式——享元模式(Flyweight Pattern)
模式动机 面向对象技术可以很好地解决一些灵活性或可扩展性问题,但在很多情况下需要在系统中增加类和对象的个数.当对象数量太多时,将导致运行代价过高,带来性能下降等问题.享元模式正是为解决这一类问题而诞生 ...
- 16 SQL Tuning Overview
16.1 Introduction to SQL Tuning Identifying high load or top SQL statements that are responsible for ...
- JAVA 使用POI导出数据格式为Execl
需要下载一个poi的jar包. 控制器 @Override public void getContractListExecl(Contract contract, BindingResult resu ...
- Easy UI
首先去Easy UI官网下载离线包 导入要用的js模块 <!DOCTYPE html> <html> <head lang="en"> < ...
- JavaWeb基础: XML基础知识
简介 XML:可扩展标记语言,W3C指定的用于描述结构化数据的语言,XML在实际开发中,经常用作软件的配置文件,以描述程序模块之间的依赖和组合关系. XML约束:XML常常用于软件配置管理,对于软件框 ...
- Java 类中各成分加载顺序 和 内存中的存放位置
参加一个笔试,有一个关于类的静态代码块.构造代码块.构造函数的执行顺序的问题.不太清楚,网上百度了一下.在这里记录一下. 一.什么时候会加载类?使用到类中的内容时加载:有三种情况1.创建对象:new ...
- How do I install Adobe Flash on Debian Wheezy?
aptitude install flashplugin-nonfreeif fail Check your settings in /etc/apt/sources.list. If this is ...
- 图的深度优先和广度优先遍历(图以邻接表表示,由C++面向对象实现)
学习了图的深度优先和广度优先遍历,发现不管是教材还是网上,大都为C语言函数式实现,为了加深理解,我以C++面向对象的方式把图的深度优先和广度优先遍历重写了一遍. 废话不多说,直接上代码: #inclu ...
- win server2008R2安装framework1.1后,在应用池中不能编辑选择framework1.1的解决办法
C:\Users\Administrator>mklink /d "c:/Windows/Microsoft.NET/Framework64/v1.1.4322" " ...
- 1410. Crack
http://acm.timus.ru/problem.aspx?space=1&num=1410 题目倒是不难,水题DP 就是题意理解起来有点困难,意思就是给你一段话,提取里面的单词 单词有 ...