-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path800.cpp
More file actions
40 lines (39 loc) · 1.21 KB
/
800.cpp
File metadata and controls
40 lines (39 loc) · 1.21 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
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
string helper(string str) {
string dict = "0123456789abcdef";
int value = stoi(str, nullptr, 16);
int index = value / 17 + (value % 17 > 8 ? 1 : 0);
return string(2, dict[index]);
}
string similarRGB(string color) {
return "#" + helper(color.substr(1, 2)) + helper(color.substr(3, 2)) + helper(color.substr(5, 2));
}
};
class Solution {
public:
string hexstring = "0123456789abcdef";
unordered_map<char, int> hexChar2int;
int hex2int(char c1, char c2) {
return hexChar2int[c1] * 16 + hexChar2int[c2];
}
string getSimilar(string s) {
int target = hex2int(s[0], s[1]);
int diff = INT_MAX;
char res;
for (auto c : hexstring) {
int current = hex2int(c, c);
if (abs(current - target) < diff) {
diff = abs(current - target);
res = c;
}
}
return string(2, res);
}
string similarRGB(string color) {
for (int i = 0; i < 16; ++i) {
hexChar2int[hexstring[i]] = i;
}
return "#" + getSimilar(color.substr(1, 2)) + getSimilar(color.substr(3, 2)) + getSimilar(color.substr(5, 2));
}
};