problem:
Given integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part was 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) ".
Analysis:
fristly, you man think it ' s a difficult problem. If you are clam down, notice it's not a hard problem, since you has already familar with its underlying algorit Hm. What are need to Dois to write a good code to solve the problem elegantly!Idea:the reason why we have a decimal part was the numerator could not being fully divided by denominator. We could divide the process into following part:1. Get quotient part.LongQuotient = dividend/Divisor;res+=quotient;2. Get decimal part. Recall we have the done through mannually. 1 .... 9 |-----------1 0 9------------1 0We need to get remainder, andifThe remainder is not equal to 0 and we multiply the remainder with 10. andContinueThe process. while(Remainder! = 0) {... map.put (remainder, res.length ()); Longde = remainder/Divisor; Res+=string.valueof (DE); Remainder= (remainder% divisor) * 10;} However, the problem asks us to circle out of the repeative part,ifThe decialmal part is infinite. How could we detect the Circle part out?Use our old friend:hashset, HashMap. And we knowifA remainder appear gain, it must is the sign of a circle!!! 1. HashSet We Indeed can use it for detect a circle, however, it has no memory of where the circle starts.2. Hashmapwe certainly can use it for detect circle, what' s more, we can record the next index of each remainder!!! Thus when we reach a circle, we can easily point the circle out.---------------------------------------------------------- --------HashMap<long, integer> map =NewHashmap<long, integer> (); while(Remainder! = 0) { if(Map.containskey (remainder)) {intBeg =map.get (remainder); Res= res.substring (0, Beg) + "(" + res.substring (Beg, Res.length ()) + ")"; returnRes; } map.put (remainder, res.length ()); Longde = remainder/Divisor; Res+=string.valueof (DE); Remainder= (remainder% divisor) * 10;}2.1record the start (next index) of each remaindermap.put (remainder, res.length ());2.2get the circular part out.res= res.substring (0, Beg) + "(" + res.substring (Beg, Res.length ()) + ")"; mistake:directly use Math.Abs over Integer.Note:when Integer=Math.min_value, Math.Abs (math.min_value) has no effect!!! Otherwise, it could cause overflow!!!Fix:store the integer into aLong, then use ABS over theLong.LongDividend =Math.Abs (numerator);Longdivisor = Math.Abs (denominator);
Solution:
Public classSolution { PublicString Fractiontodecimal (intNumerator,intdenominator) { if(Numerator = = 0) return"0"; if(Denominator = = 0) return""; String Res= ""; if((Numerator < 0) ^ (Denominator < 0)) Res+ = '-'; LongDividend =numerator; Longdivisor =denominator; Dividend=Math.Abs (dividend); Divisor=Math.Abs (divisor); LongQuotient = dividend/Divisor; Res+=quotient; Longremainder = (dividend% divisor) * 10; if(remainder = = 0) returnRes; Res+ = '. '; HashMap<long, integer> map =NewHashmap<long, integer> (); while(Remainder! = 0) { if(Map.containskey (remainder)) {intBeg =map.get (remainder); Res= res.substring (0, Beg) + "(" + res.substring (Beg, Res.length ()) + ")"; returnRes; } map.put (remainder, res.length ()); Longde = remainder/Divisor; Res+=string.valueof (DE); Remainder= (remainder% divisor) * 10; } returnRes; }}
[leetcode#116] Fraction to recurring Decimal