166. Fraction to Recurring Decimal

166. Fraction to Recurring Decimal

Description

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part is repeating, enclose the repeating part in parentheses.

If multiple answers are possible, return any of them .

It is guaranteed that the length of the answer string is less than 10^4 for all the given inputs.

Example 1:

1
2
Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:

1
2
Input: numerator = 2, denominator = 1
Output: "2"

Example 3:

1
2
Input: numerator = 4, denominator = 333
Output: "0.(012)"

Constraints:

  • -2^31 <=numerator, denominator <= 2^31 - 1
  • denominator != 0

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) {
return "0";
}
string res;
if (numerator < 0 ^ denominator < 0) {
res.push_back('-');
}
long dividend = labs(numerator);
long divisor = labs(denominator);
res.append(to_string(dividend / divisor));
if (dividend % divisor == 0) {
return res;
}
dividend = dividend % divisor;
unordered_map<int, int> previous;
res.push_back('.');
int digits = 1;
while (dividend != 0) {
if (previous.contains(dividend)) {
res.insert(previous[dividend], "(");
res.push_back(')');
return res;
}
previous[dividend] = res.size();
dividend *= 10;
res.append(to_string(dividend / divisor));
dividend %= divisor;
}
return res;
}
};