Topic:
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)".
Ideas:
Using a map to record each remainder, when there is a duplicate remainder, then the loop is entered, and the part between the two repeating remainder is the loop body.
C + + code:
classsolution{ Public:stringFractiontodecimal (intNumerator,intDenominator) {if(Numerator = =0)return "0";if(Denominator = =0)return "";stringresult ="";//If one of the numbers is negative, the result adds a minus sign anyway if((Numerator <0) ^ (Denominator <0)) Result + ="-";//Absolute values for numerator and denominator Long LongLnumerator= numerator;Long LongLdenominator = denominator; Lnumerator =ABS(Lnumerator); Ldenominator =ABS(Ldenominator);Long Longquotient = Lnumerator/ldenominator; Result + = to_string (quotient);//The remainder is multiplied by 10 for subsequent calculation of the remainder of the result as an integer Long Longremainder = lnumerator% Ldenominator *Ten;//If the remainder is 0, return the result directly if(Remainder = =0)returnResult Result + ="."; unordered_map<long long, int>Dictionary//For storing the remainder and the remaining number of subscripts while(Remainder! =0) {//If this remainder exists in the dictionary if(Dictionary.find (remainder)! = Dictionary.end ()) {intPosition = Dictionary[remainder];stringFront = RESULT.SUBSTR (0, position);stringback = Result.substr (position, result.length ());returnFront +"("+ Back +")"; } dictionary.insert ({remainder, result.length ()}); quotient = Remainder/ldenominator;//With short Division calculatorResult + = to_string (quotient); remainder = remainder% Ldenominator *Ten;//Get the remainder and multiply by ten}returnResult }};
Java code (because the problem does not provide a test of C # code, so using Java here, the syntax differences of the two languages are not too large):
Public classSolution { PublicStringFractiontodecimal(intNumerator,intDenominator) {if(Numerator = =0)return "0";if(Denominator = =0)return ""; StringBuilder result =NewStringBuilder ();//If one of the numerator and denominator is negative, the result adds a minus sign if((Numerator >0) ^ (Denominator >0)) Result.append ("-");//give numerator and denominator absolute value LongLnumberator = numerator;LongLdenominator = denominator; Lnumberator = Math.Abs (lnumberator); Ldenominator = Math.Abs (ldenominator);//Calculate integer part Longquotient = Lnumberator/ldenominator; Result.append (quotient);//The remainder is multiplied by 10 to allow the calculation of the residual part of the back Longremainder = lnumberator% Ldenominator *Ten;//If the remainder is 0, the result is returned directly if(Remainder = =0)returnResult.tostring (); Result.append ('. '); Map<long, Integer> dictionary =NewHashmap<long, integer> (); while(Remainder! =0) {//If the value of remainder exists in the dictionary, the number of decimal parts appears in the Loop if(Dictionary.containskey (remainder)) {intPosition = dictionary.Get(remainder); String front = result.substring (0, position); String back = result.substring (position, result.length ());returnFront +' ('+ Back +' ) '; }//Add remainder to Dictionary (key is remainder,value for corresponding position in result), continue to calculate remainderDictionary.put (remainder, result.length ()); quotient = Remainder/ldenominator; Result.append (quotient); remainder = remainder% Ldenominator *Ten; }returnResult.tostring (); }}
Python code:
Class Solution:# @return A stringdef fractiontodecimal (self, numerator, denominator):ifNumerator = =0:return ' 0 ' ifDenominator = =0:return "' result="' if(Numerator >0) ^ (Denominator >0):result=result+'-'Pnumerator =ABS(numerator) Pdenominator =ABS(denominator) quotient = Pnumerator/pdenominatorresult=result+ str (quotient) remainder = pnumerator% Pdenominator *Ten ifremainder = =0:return result result=result+'. 'Map = {} whileRemainder! =0:ifMap.has_key (remainder): position = Map[remainder] Front =result[0: position] Back =result[Position:Len(result)]returnFront +' ('+ Back +' ) 'Map[remainder] =Len(result) quotient = Remainder/pdenominatorresult=result+ str (quotient) remainder = remainder% Pdenominator *Ten return result
Leetcode:fraction to recurring Decimal