"Leetcode hash Table" Fraction to recurring Decimal

Source: Internet
Author: User

"leetcode hash table" fraction to recurring Decimal

@author: Wepon
@blog: http://blog.csdn.net/u012162613


1. Topics

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) ".

2. Analysis Test Instructions: given two integers, one representing the numerator numerator, one representing the denominator denominator, the result of returning them as decimals, as shown in parentheses when there is repeating decimal. such as 5/3=1.666666 ... with "1." (6) "Returns the type of a string.
key points to solve:1, the processing of negative numbers2, Int_min processing, convert int_min to positive will overflow, so to use a long long INT to calculate. 3, is divided into the integer part and the fractional part, the emphasis lies in the fractional part processing, because the fractional portion may appear the circulation. Observe the following division:
when the remainder is 4 o'clock, a loop occurs, so we can set a hash table, store the remainder of each time, and the remainder in the return result of the subscript. Each time a new remainder is obtained, it is queried whether the remainder is already in the hash table, and if it is the beginning of the loop, then insert ' (', end of result plus ') ' at the end of the remainder corresponding to the result, ending the operation. If it is not found in the hash table, the normal operation continues.
3. Code
Class Solution {public:string fractiontodecimal (int numerator, int denominator) {if (numerator==0) return "0";        string result;   if (numerator<0 ^ denominator<0) result+= '-'; XOR, Numerator<0 and denominator<0 only one for true//conversion to positive, int_min conversion to positive will overflow, so with a long long.        A long long int n=abs (int_min) Gets the n is still negative, so it is written in the following form.        Long long int n=numerator,d=denominator;                              N=abs (n);d =abs (d);  Result+=to_string (N/D);    Integer part of long long int r=n%d;        Remainder R if (r==0) return result;        Else result+= '. ';        The following processing of fractional parts, with a hash table unordered_map<int,int> map; while (r) {//checks if the remainder r is in the hash table, then starts looping if (Map.find (R)!=map.end ()) {Result.insert (map[r],1   ,'(');                http://www.cplusplus.com/reference/string/basic_string/insert/result+= ') ';            Break    } map[r]=result.size ();          The remainder corresponds to which position of the result//normal operation r*=10;  Result+=to_string (R/D);        r=r%d;    } return result; }};


"Leetcode hash Table" Fraction to recurring Decimal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.