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 ...
随机推荐
- Solr -- 实时搜索
在solr中,实时搜索有3种方案 ①soft commit,这其实是近实时搜索,不能完全实时. ②RealTimeGet,这是实时,但只支持根据文档ID的查询. ③和第一种类似,只是触发softcom ...
- office 软件常用备忘(删除线/冻结等)
Word: 1 大纲显示/navigation display: 视图/View --> Navigation Pane (left) 2 Track changes: 显示删除线 / 修改地方 ...
- POJ-2352 Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...
- “面向对象"和"面向过程"到底有什么区别?
链接:http://www.zhihu.com/question/27468564/answer/101951302 当软件还非常简单的时候,我们只需要面向过程编程: 定义函数函数一函数二函数三函数四 ...
- groovy-集合
Lists 你能使用下面的方法创建一个lists,注意[]是一个空list. 1 def list = [5, 6, 7, 8] 2 assert list.get(2) == 7 3 assert ...
- 在 ASP.NET MVC 3 中应用 KindEditor
http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html 第一步 将 KindEditor 的源文件添加到项目中,建议放到 /Scr ...
- AndroidManifest File Features
http://www.android-doc.com/guide/topics/manifest/manifest-intro.html The following sections describe ...
- MyISAM和InnoDB的索引在实现上的不同
1 MyISAM只把索引载入内存,数据缓存依赖于操作系统,InnoDB把索引和数据都载入内存缓冲 2 MyISAM数据库中的数据是按照插入的顺序保存,在每个索引节点中保存对应的数据行的地址,理论上说主 ...
- HttpClient教程
2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大.但是如果我们直接使用已 ...
- mac 下终端访问文件出现“Permission Denied”解决方案
mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...