Fraction to Recurring Decimal
2015-08-06 17:09
225 查看
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)".
【题意】
给定一个分子和一个分母,以字符串的形式返回该小数。如果小数无限循环的话,用括号扩住循环体。
【官方Solution】
The key insight here is to notice that once the remainder starts repeating, so does the divided result.
You will need a hash table that maps from the remainder to its position of the fractional part. Once you found a repeating remainder, you may enclose the reoccurring fractional part with parentheses by consulting the position from the table.
The remainder could be zero while doing the division. That means there is no repeating fractional part and you should stop right away.
Just like the question Divide Two Integers, be wary of edge case such as negative fractions
and nasty extreme case such as
【中文解析】
难点:如何识别循环体?
解决方法:用一个HashMap记录每一个余数,当出现重复的余数时,那么将会进入循环,两个重复余数之间的部分就是循环体。
示例:1/13=0.076923076923076923...,当小数部分第二次出现0时,就意味着开始了循环,那么需要把076923用括号括起来,结果为0.(076923)。
涉及技巧:1)在不断相除的过程中,把余数乘以10再进行下一次相除,保证一直是整数相除;2)HashMap的key和value分别是<当前余数, 对应结果下标>,这样获取076923时就可根据value值来找。
注意点1:考虑正负数,先判断符号,然后都转化为正数;
注意点2:考虑溢出,如果输入为Integer.MIN_VALUE,取绝对值后会溢出。
代码如下:
参考博客:http://blog.csdn.net/ljiabin/article/details/42025037,分析的很详细
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)".
【题意】
给定一个分子和一个分母,以字符串的形式返回该小数。如果小数无限循环的话,用括号扩住循环体。
【官方Solution】
0.16 6 ) 1.00 0 1 0 <-- Remainder=1, mark 1 as seen at position=0. - 6 40 <-- Remainder=4, mark 4 as seen at position=1. - 36 4 <-- Remainder=4 was seen before at position=1, so the fractional part which is 16 starts repeating at position=1 => 1(6).
The key insight here is to notice that once the remainder starts repeating, so does the divided result.
You will need a hash table that maps from the remainder to its position of the fractional part. Once you found a repeating remainder, you may enclose the reoccurring fractional part with parentheses by consulting the position from the table.
The remainder could be zero while doing the division. That means there is no repeating fractional part and you should stop right away.
Just like the question Divide Two Integers, be wary of edge case such as negative fractions
and nasty extreme case such as
-2147483648 / -1.
【中文解析】
难点:如何识别循环体?
解决方法:用一个HashMap记录每一个余数,当出现重复的余数时,那么将会进入循环,两个重复余数之间的部分就是循环体。
示例:1/13=0.076923076923076923...,当小数部分第二次出现0时,就意味着开始了循环,那么需要把076923用括号括起来,结果为0.(076923)。
涉及技巧:1)在不断相除的过程中,把余数乘以10再进行下一次相除,保证一直是整数相除;2)HashMap的key和value分别是<当前余数, 对应结果下标>,这样获取076923时就可根据value值来找。
注意点1:考虑正负数,先判断符号,然后都转化为正数;
注意点2:考虑溢出,如果输入为Integer.MIN_VALUE,取绝对值后会溢出。
代码如下:
public class Solution { public String fractionToDecimal(int numerator, int denominator) { if(numerator==0) return "0"; if(denominator==0) return ""; //用来记录相除后的结果 String result = ""; //如果结果为负数 if((numerator<0)^(denominator<0)){ result += "-"; } //将分子分母转为正数,用Long防止Integer.MIN这个数无法转换 long numer = numerator; long deno = denominator; numer = Math.abs(numer); deno = Math.abs(deno); long res = numer/deno; result += res; long rem = (numer%deno)*10; if(rem==0){ return result; } result += "."; HashMap<Long,Integer> map = new HashMap<>(); while(rem!=0){ if(map.containsKey(rem)){ int beg = map.get(rem); int end = result.length(); String s1 = result.substring(0,beg); String s2 = result.substring(beg,end); result = s1+"("+s2+")"; return result; } map.put(rem,result.length()); res = rem/deno; result +=res; rem = (rem%deno)*10; } return result; } }
参考博客:http://blog.csdn.net/ljiabin/article/details/42025037,分析的很详细
相关文章推荐
- 优步UBER司机全国各地奖励政策汇总:北京、上海、广州、深圳、佛山、天津、南京、武汉、成都、重庆、济南、西安、宁波、青岛、长沙、苏州
- datatable1.9 与datatable1.10以数据差异
- 启示—地点IT高管20在职场心脏经(读书笔记6)
- Rendering Problems:android.support.v7.internal.widget.ActionBarOverlayLayout 解决方法
- 浅谈容器
- sgu 180 - Inversions (离散化+树状数组)
- 15期.net 博客汇总
- bootstrap模态框打开报错
- jQuery几个重要的实际应用技巧
- NavigationBar的第二中方法,图片变色,文字不变色
- ffmpeg 怎么用
- P - 奔小康赚大钱 - hdu 2255(带权值的匹配)
- mongodb查询的语法(大于,小于,大于或等于,小于或等于等等)
- ant打包
- UVA 639 Don't Get Rooked
- poj-1125 Stockbroker Grapevine
- Android 自定义View 视频音量调控
- SPOJ NWERC11E Please, go first
- 树的子结构
- 动软代码生成器 可用于生成Entity层,可更改模板 /codesmith 也可以