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)".
分析:https://segmentfault.com/a/1190000003794677
哈希表法
复杂度
时间 O(N) 空间 O(N)
思路
整数部分很好处理,只要注意正负号的区分就行了,但是如何处理小数部分呢。如果只是简单的除法,那我们每次把余数乘以10,再除以被除数就可以得到当前位的小数了,得到新的余数,直到余数为0。难点在于,对于无尽循环小数,我们一直这么做永远也不能让余数变为0。这里我们可以用一个哈希表记录每次的余数,如果余数出现重复的时候,说明就产生循环了。为了能找出小数中循环的部分,我们在用哈希表时,还要把每个余数对应的小数位记录下来,这样子我们一旦遇到重复,就知道是从哪里开始循环的。
注意
如果输入的被除数很大,那么余数乘以10有可能溢出,所以我们用long来保存numerator和denominator。
public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
long num = numerator, den = denominator;
if (num == || den == ) return "";
boolean negative = (num > && den < ) || (num < && den > );
num = Math.abs(num);
den = Math.abs(den);
String integ = (negative ? "-" : "") + String.valueOf(num / den);
num = num % den;
if (num != ) {
HashMap<Long, Integer> map = new HashMap<>();
int pos = ;
StringBuilder frac = new StringBuilder();
while (num != ) {
map.put(num, pos);
num = num * ;
frac.append(num / den);
num = num % den;
if (map.containsKey(num)) {
// 将非循环部分和循环部分分开
String pre = frac.substring(, map.get(num));
String loop = frac.substring(map.get(num));
return integ + "." + pre + "(" + loop + ")";
}
pos++;
}
return integ + "." + frac.toString();
}
return integ;
}
}
Fraction to Recurring Decimal的更多相关文章
- 【leetcode】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 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- 【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 Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
- [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
随机推荐
- 【Delphi】GIF 动画建立
var Gif:TGifImage; begin //Setting the delay for each frame TGIFGraphicControlExtension.; TGIFGraphi ...
- jquery 匿名函数的区别
下面两个定义函数的方法是等价的 var test = function(val){alert(val);} function test(val){alert(val);} 都是定义了一个test()方 ...
- springmvc请求参数获取的几种方法
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...
- Java并发编程核心方法与框架-Fork-Join分治编程(一)
在JDK1.7版本中提供了Fork-Join并行执行任务框架,它的主要作用是把大任务分割成若干个小任务,再对每个小任务得到的结果进行汇总,这种开发方法也叫做分治编程,可以极大地利用CPU资源,提高任务 ...
- jQuery EasyUI API 中文文档 - ValidateBox验证框
jQuery EasyUI API 中文文档 - ValidateBox验证框,使用jQuery EasyUI的朋友可以参考下. 用 $.fn.validatebox.defaults 重写了 d ...
- mysql 简单练习
1.查找全部学生的信息 [SQL]select * from student 受影响的行: 0 时间: 0.000s 2.查出成绩及格的所有人 [SQL]select * from student w ...
- 如何判断一个变量是否是utf-8
//判断传入的字符是否是utf-8 function is_utf8($word){ if (preg_match("/^([".chr(228)."-" ...
- List之Union(),Intersect(),Except()
http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800202.html List之Union(),Intersect(),Except() ...
- 初探Ajax
1.什么是Ajax Ajax是Asynchronous JavaScript and XML的缩写,这一技术能从服务器请求额外数据而无需卸载页面.传统的HTTP请求流程大概是这样的,浏览器向服务器发送 ...
- CSS 补充
属性选择器下面的例子为带有 title 属性的所有元素设置样式:[title]{ color:red;} <h1>可以应用样式:</h1><h2 title=" ...