-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
29 lines (29 loc) · 857 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/similar-rgb-color/
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
private:
const string digits = "0123456789abcdef";
public:
string similarRGB(string color) {
string ans = "#";
for (int i = 0; i < 3; ++i) {
char a = color[1 + i * 2], b = color[2 + i * 2];
int hex1 = stoi(color.substr(1 + i * 2, 2), nullptr, 16);
int best = 0, bestSim = INT_MIN;
for (int j = 0; j < 16; ++j) {
int hex2 = j * 16 + j;
int sim = -pow(hex1 - hex2, 2);
if (sim > bestSim) {
bestSim = sim;
best = j;
}
}
char c = digits[best];
ans += c;
ans += c;
}
return ans;
}
};