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 ...
随机推荐
- jQuery 文本编辑器插件 HtmlBox 使用
0.htmlbox下载地址:http://download.csdn.net/detail/leixiaohua1020/6376479 1.引入头文件 <script src="li ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- mysql中的having
from子句后面可以用where选择行,group by子句后面可以用having子句选择行, having中条件的定义和where中很相似,但是having中可以直接用聚合函数,但是where中不能 ...
- POJ3714 Raid
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10625 Accepted: 3192 Description ...
- vim YouCompleteMe
http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html
- sixsix团队“餐站”应用M2阶段发布报告
一.新功能 客户端 搜索功能 我们在M2中实现了对地点的搜索菜品,可以直接在主页页面中的输入框输入用户喜欢的菜品,系统将返回与对应关键字所对应的选择,更加高效直观的满足客户的口味. 菜品图片加载 我们 ...
- ubuntu设置开机启动图形应用程序,替换默认图形桌面
直接将启动程序放在rc.local即可.但是如果自动启动的程序奔溃后,会返回到ubuntu的unity桌面系统. 我遇到的问题是程序还有调用 xset 去定时关闭屏幕.在桌面启动后调用没问题.如果rc ...
- Ubuntu学习总结-01 用VMware 8安装Ubuntu 12.04详细过程
1 Ubuntu 下载地址 http://www.ubuntu.com/download/desktop 2 安装Ubuntu 转载用VMware 8安装Ubuntu 12.04详细过程 http:/ ...
- jquery------脚注的使用
index.jsp <script type="text/javascript" src="../js/my.js"></script> ...
- Laravel教程 八:queryScope 和 setAttribute
Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...