[leetcode]166. Fraction to Recurring Decimal

来源:互联网 发布:长城宽带 端口转发 编辑:程序博客网 时间:2024/06/05 22:49

题目链接:https://leetcode.com/problems/fraction-to-recurring-decimal/description/

Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the decimal parts to recur, the remainders should recur. So we need to maintain the remainders we have seen. Once we see a repeated remainder, we know that we have reached the end of the recurring parts and should enclose it with a ). However, we still need to insert the ( to the correct position. So we maintain a mapping from each remainder to the position of the corresponding quotient digit of it in the recurring parts. Then we use this mapping to retrieve the starting position of the recurring parts.

Now we have solved the trickiest part of this problem.

There are some remaining problems to solve to achieve a bug-free solution.

  1. Pay attention to the sign of the result;
  2. Handle cases that may cause overflow like numerator = -2147483648, denominator = -1 appropriately by using long long;
  3. Handle all the cases of (1) no fractional part; (2) fractional part does not recur; and (3) fractional part recurs respectively.

To handle problem 3, we divide the division process into the integral part and the fractional part. For the fractional part, if it does not recur, then the remainder will become 0 at some point and we could return. If it does recur, the method metioned in the first paragraph has already handled it.


class Solution {public:    string fractionToDecimal(int numerator, int denominator) {        if (!numerator) return "0";        string res;        if(numerator<0 ^ denominator<0) res+='-';        long long numer=numerator<0?(long long)numerator*(-1):(long long)numerator;        long long denom=denominator<0?(long long)denominator*(-1):(long long)denominator;        long long integral=numer/denom;        res+=to_string(integral);        long long rmd=numer%denom;        if(!rmd) return res;        res+='.';        rmd*=10;        unordered_map<long long,long long> mp;        while(rmd)        {            long long quotient=rmd/denom;            if(mp.find(rmd)!=mp.end())            {                res.insert(mp[rmd],1,'(');                res+=')';                break;            }            mp[rmd]=res.size();            res+=to_string(quotient);            rmd=(rmd%denom)*10;        }        return res;    }};



阅读全文
0 0
原创粉丝点击