✡ 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 ...
随机推荐
- Linux信号基础
Linux信号基础 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Linux进程基础一文中已经提到,Linux以进程为单位来 ...
- Redis - 发布和订阅
一.概述 1). 发布和订阅是一种消息通信模式. 2). 优点:使消息订阅者和消息发布者耦合度降低,类似设计模式中的观察者模式. 二.发布和订阅 订阅命令: // 订阅一个或多个频道 // 返回值:v ...
- mysql exists 和 in的效率比较
这条语句适用于a表比b表大的情况 select * from ecs_goods a where cat_id in(select cat_id from ecs_category b); 这条语句适 ...
- nginx+tomcat 配置负载均衡
nginx 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本(我用的是nginx-1.8.1版本) 安装就直接把压缩包解压到一个路径 ...
- django中“url映射规则”和“服务端响应顺序”
1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单. 查看方式: import sys print sys.path ...
- EaseType缓动函数
http://sol.gfxile.net/interpolation/ 一篇很详细的图文
- Windows C++ 子目录数量
CFileFind OneFile; BOOL bWorking = FALSE; ; ) != "\\") { strPath += "\\*.*"; } b ...
- 张艾迪(创始人):Hello.世界...
The World No.1 Girl :Eidyzhang The World No.1 Internet Girl :Eidyzhang AOOOiA.global Founder :Eidyzh ...
- 开发实时壁纸(Live Wallpapers)
所谓实时壁纸,就是指手机桌面不再是简单的图片,而是运行中的动画,这个动画是由程序实时绘制的,因此被称为实时壁纸. 为了开发实时壁纸,Android提供了WallpaperService基类,实时壁纸的 ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树
E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...