[Leetcode] 166. Fraction to recurring Decimal problem solving report

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/fraction-to-recurring-decimal/

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

Hint:

    1. No scary Math, just apply elementary math knowledge. Still remember how to perform a long division?
    2. Try a long division on 4/9, and the repeating part is obvious. Now try 4/333. Do you see a pattern?
    3. Be wary of Edge cases! List out as many test cases as can think of and test your code thoroughly.

Ideas: There are several situations:

1. If it can be done directly, then it is best to return directly

2. If it cannot be done directly, take an integer first and then calculate the decimal number. The calculation of the number of minutes is the time of each pick-up, but also can be divided into can be exhausted, and repeating decimal

1. Return results if you can divide the end evenly

2. The trouble is repeating decimal. We need to save each dividend and the position of the current quotient with a hash table, so that when one finds the same

The dividend indicates that there is a loop, then add parentheses to the position of the quotient where the divisor occurred for the first time.

And it may be out of bounds when doing the operation, so we are going to store the divisor and dividend in a long type, and extract the symbol first before the operation.

I'm so dizzy with my head! ^.^

The code is as follows:

Class Solution {public:    string fractiontodecimal (int numerator, int denominator) {        if (numerator = = 0) return "0";        long M=labs (numerator), n=labs (denominator);        Unordered_map<int, int> hash;        int k=numerator>0 ^ denominator>0, i = 0;         string result;        if (m% n = = 0) return k==0?to_string (m/n): "-" +to_string (m/n);        result = To_string (m/n) + ".";        i = Result.size ();        m = m% n * TEN;        while (Hash.find (m) = = Hash.end ())        {            hash[m] = i++;            if (m% n ==0)            {                result + = to_string (m/n);                Return K==0?result: "-" +result;            }            Result + = to_string (m/n);            m = m%n*10;        }        Result.insert (Hash[m], 1, ' (');        result+= ")";        Return K==0?result: "-" +result;    };











[Leetcode] 166. Fraction to recurring Decimal problem solving report

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.