-
Notifications
You must be signed in to change notification settings - Fork 1
/
166.cpp
51 lines (43 loc) · 1.28 KB
/
166.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
// 166. Fraction to Recurring Decimal - https://leetcode.com/problems/fraction-to-recurring-decimal
#include "bits/stdc++.h"
#include "gtest/gtest.h"
using namespace std;
typedef long long ll;
class Solution {
public:
// upgraded parameter types
string fractionToDecimal(ll n, ll d) {
if (n == 0) return "0";
string result;
if (n < 0 ^ d < 0) result += '-';
n = abs(n), d = abs(d);
result += to_string(n / d);
if (n % d == 0) return result;
result += '.';
// rem -> idx
unordered_map<ll, ll> map;
// Simulate the division process:
for (ll r = n % d; r; r %= d) {
if (map.count(r) > 0) {
result.insert(map[r], 1, '(');
result += ')';
break;
}
map[r] = result.size();
r *= 10;
result += to_string(r / d);
}
return result;
}
};
TEST(SolutionTest, Small) {
Solution sol;
EXPECT_EQ("0.5", sol.fractionToDecimal(1, 2));
EXPECT_EQ("2", sol.fractionToDecimal(2, 1));
EXPECT_EQ("0.(6)", sol.fractionToDecimal(2, 3));
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}