-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path166.cpp
More file actions
29 lines (26 loc) · 1.04 KB
/
166.cpp
File metadata and controls
29 lines (26 loc) · 1.04 KB
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
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
long long n = numerator;
long long d = denominator;
if(n % d == 0) return to_string(n/d);
string ans = to_string(n/d) + ".";
if(n/d == 0 && d/n < 0) ans = "-" + ans; //edge case where n/d = 0 doesn't format properly
n = (n%d)*10;
vector<char> decimal;
unordered_map<long long, int> m; //{remainder, index of first appearance}
int pos = -1; //position of left parenthesis
while(n != 0){
if(m.count(n) > 0){ //already seen this remainder before
pos = m[n];
break;
}
m[n] = decimal.size();
decimal.push_back(abs(n/d) + '0');
n = (n%d)*10;
}
if(pos < 0) return (ans + string(decimal.begin(), decimal.end()));
else return (ans + string(decimal.begin(), decimal.begin()+pos)
+ "(" + string(decimal.begin()+pos,decimal.end()) +")");
}
};