-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path166.cpp
122 lines (116 loc) · 2.51 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
__________________________________________________________________________________________________
4ms
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if (numerator == 0)
return "0";
string result = "";
if (numerator < 0 ^ denominator < 0)
result += "-";
long n = labs(numerator);
long d = labs(denominator);
result += to_string(n / d);
long r = n % d;
if (r == 0)
return result;
result += ".";
unordered_map<int, int> last_position;
while (r != 0) {
if (r != 0 && last_position.find(r) != last_position.end()) {
result.insert(last_position[r], "(");
result += ")";
return result;
}
else {
last_position[r] = result.size();
}
r *= 10;
result += to_string(r / d);
r = r % d;
}
return result;
}
};
__________________________________________________________________________________________________
8540 kb
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if (!numerator || !denominator)
return string("0");
bool minus = (numerator < 0) ^ (denominator < 0);
long long q = ((long long)numerator) * (minus ? -1 : 1) / denominator;
long long r = ((long long)numerator) * (minus ? -1 : 1) % denominator;
vector<long long> vQs;
vector<int> vRs;
vQs.push_back(q);
if (r)
vRs.push_back(r);
bool bRecurr = false;
vector<int>::iterator it;
while (r)
{
r = r * 10;
q = r / denominator;
r = r % denominator;
vQs.push_back(q);
if (!r)
break;
else if ((it = find(vRs.begin(), vRs.end(), r)) != vRs.end())
{
bRecurr = true;
break;
}
vRs.push_back(r);
}
//
string ret;
ret.append(itoa(vQs.front()));
if (!vRs.empty())
{
ret.append(1, '.');
for (int i = 1; i < vQs.size(); i++)
{
if (bRecurr && vRs[i - 1] == *it)
ret.append(1, '(');
ret.append(itoa(vQs[i]));
}
if (bRecurr)
ret.append(1, ')');
}
if (minus)
ret = string("-") + ret.c_str();
return ret;
}
char * itoa(long long num)
{
static char buffer[32];
memset(buffer, NULL, sizeof(buffer));
//
int i = 0;
if (num < 0)
{
buffer[0] = '-';
num = num * -1;
i++;
}
do
{
buffer[i++] = (num % 10) + '0';
num = num / 10;
} while (num);
// reverse
int begin = 0, end = i - 1;
while (begin < end)
{
char tmp = buffer[begin];
buffer[begin] = buffer[end];
buffer[end] = tmp;
begin++;
end--;
}
return buffer;
}
};
__________________________________________________________________________________________________