classSolution { public: string fractionToDecimal(int numerator, int denominator){ long n = numerator, d = denominator; if(n % d == 0) returnto_string(n / d);
string res = ""; if(n * d < 0) res += '-'; n = abs(n), d = abs(d); res += to_string(n / d) + '.'; n %= d; unordered_map<int ,int> mp; int len = res.size(); while(n && !mp.count(n)){ mp[n] = len ++; n *= 10; res += to_string(n/d); n %= d; }