Skip to content

Commit a01ab93

Browse files
authored
Create similar-rgb-color.cpp
1 parent 0e0754a commit a01ab93

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/similar-rgb-color.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string similarRGB(string color) {
7+
string result = "#";
8+
result += round(color.substr(1, 2));
9+
result += round(color.substr(3, 2));
10+
result += round(color.substr(5, 2));
11+
return result;
12+
}
13+
14+
private:
15+
string round(const string& color) {
16+
std::stringstream in(color);
17+
int decimal = 0;
18+
in >> std::hex >> decimal;
19+
decimal = decimal / 17 + (decimal % 17 > 8 ? 1 : 0);
20+
std::stringstream out;
21+
out << std::setfill('0') << std::setw(2) << std::hex << 17 * q;
22+
return out.str();
23+
}
24+
};

0 commit comments

Comments
 (0)