-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path166. Fraction to Recurring Decimal.cpp
66 lines (50 loc) · 1.41 KB
/
166. Fraction to Recurring Decimal.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Link: https://leetcode.com/problems/fraction-to-recurring-decimal/
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.
Example 1:
Input: numerator = 1, denominator = 2
Output: "0.5"
Example 2:
Input: numerator = 2, denominator = 1
Output: "2"
Example 3:
Input: numerator = 2, denominator = 3
Output: "0.(6)"
*/
// Solution
// Uses the approach of long division method.
/*
TC = O(n)
SC = O(n)
*/
class Solution {
public:
string fractionToDecimal(long long int num, long long int den) {
if(num == 0)
return "0";
map<int, int> mp;
string ans = "";
if ((num < 0) ^ (den < 0))
ans.push_back('-');
ans += to_string(abs(num / den));
num = num % den;
long long int deno = abs(den);
long long int nume = abs(num);
if (num == 0)
return ans;
ans += '.';
while (nume != 0) {
if (mp.find(nume) !=mp.end()) {
ans.insert(mp[nume], "(");
ans.push_back(')');
break;
}
mp[nume] = ans.size();
nume *= 10;
ans += to_string(nume / deno);
nume %= deno;
}
return ans;
}
};