We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0e0754a commit a01ab93Copy full SHA for a01ab93
C++/similar-rgb-color.cpp
@@ -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