Java for LeetCode 166 Fraction to Recurring Decimal
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)".
解题思路:
测试中会出现各种临界值,因此,需要先将 numerator 和 denominator转为long,然后计算出整数部分,对于小数部分,将numerator*10后处理,一方面将计算除后的值放到一个list中,另一方面将新的分子放到map里,由于新分子肯定小于denominator,所以肯定会出现0或者重复(因此分数要么有限,要么是无限循环小数)一旦新分母重复出现,意味着结果会出现循环值,将其按照规则写出来即可,JAVA实现如下:
public String fractionToDecimal(int numerator, int denominator) {
StringBuilder sb = new StringBuilder();
if (numerator < 0 && denominator > 0)
sb.append("-");
else if (numerator > 0 && denominator < 0)
sb.append("-");
Long Lnumerator = (long) numerator;
if (Lnumerator < 0)
Lnumerator = -Lnumerator;
Long Ldenominator = (long) denominator;
if (Ldenominator < 0)
Ldenominator = -Ldenominator;
if (Lnumerator % Ldenominator == 0){
sb.append(Lnumerator / Ldenominator);
return sb.toString();
}
sb.append(Lnumerator / Ldenominator + ".");
System.out.println(Lnumerator);
Lnumerator %= Ldenominator;
System.out.println(Lnumerator);
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
map.put(Lnumerator, 0);
while (true) {
Lnumerator *= 10;
while (Lnumerator < Ldenominator) {
list.add(0);
map.put(Lnumerator, list.size());
Lnumerator *= 10;
}
list.add((int) (Lnumerator / Ldenominator));
Lnumerator %= Ldenominator;
if (Lnumerator == 0) {
for (int i = 0; i < list.size(); i++)
sb.append(list.get(i));
return sb.toString();
} else if (map.containsKey(Lnumerator)) {
for (int i = 0; i < map.get(Lnumerator); i++)
sb.append(list.get(i));
sb.append("(");
for (int i = map.get(Lnumerator); i < list.size(); i++)
sb.append(list.get(i));
sb.append(")");
return sb.toString();
}
map.put(Lnumerator, list.size());
}
}
Java for LeetCode 166 Fraction to Recurring Decimal的更多相关文章
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 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 ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
随机推荐
- 宿主机( win 7 系统) ping 虚拟机VMware( cent os 6.6 ) 出现“请求超时”或者“无法访问目标主机”的解决方法
首先虚拟机的网络连接设置为"Host-only": 然后在 cmd 窗口中查看 VMnet1 的 ip 地址,这里是 192.168.254.1 接下来在 Linux 中设置网卡地 ...
- .net 时间戳互相转换(精确到毫秒)
这里记录一个时间戳的互相转换方法,网上都找了,基本都没有精确到毫秒,我的这个基本可以满足精确到毫秒的级别,代码如下: /// <summary> /// Unix时间戳转换为DateTim ...
- eclipse下环境变量设置:eclipse导入工程出现 Unbound classpath variable Error
在导入网友提供的Tomcat源码工程时候出现了 The project cannot be build until build path errors are resolved Unbound cla ...
- Oracle闪回技术之一Oracle 11g 利用FlashTable (闪回表)恢复(用delete)误删的数据
闪回表,实际上就是将表中的数据快速恢复到过去的一个时间点或者系统改变号SCN上.实现表的闪回,需要用到撤销表空间相关的UNDO信息,通过SHOW PARAMETER UNDO命令就可以了解这些信息.用 ...
- Fast-cgi cgi nginx php-fpm 的关系 (转
Fast-cgi cgi nginx PHP-fpm 的关系 Fast-cgi是由cgi发展而来,是http服务器(http,nginx等)和动态脚本语言(php,perl等)之间的的通信接口, ...
- UVa OJ 180 - Eeny Meeny
Time limit: 3.000 seconds限时3.000秒 Problem问题 In darkest <name of continent/island deleted to preve ...
- pthread clean up
https://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part4/ http://www.cnblogs.com/xfi ...
- struts+spring action应配置为scope="prototype"
truts+spring action应配置为scope="prototype" <bean id="personAction" scope=" ...
- fatl exception occurred异常/错误的一种可能情况
如果,有可能是 java.lang.ClassLoader类内部出错,请自行检查
- linux基本命令(1)-用户和组管理
1.初始化Root密码 sudo passwd 2.切换至Root用户 su - root 或 sudo - i