Similar RGB Color Solutions in C++
Number 800
Difficulty Easy
Acceptance 61.5%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/similar-rgb-color/description//// Author : liuyubobobo/// Time : 2018-03-17#include <iostream>#include <cassert>using namespace std;/// Brute Force/// Time Complexity: O(16*16*16*6)/// Space Complexity: O(1)class Solution {public:string similarRGB(string color) {string color_s = "0123456789abcdef";string res = "#######";int best = INT_MAX;for(char c1: color_s)for(char c2: color_s)for(char c3: color_s){int diff = get_diff(color, c1, c2, c3);if(diff < best){best = diff;res[1] = c1;res[2] = c1;res[3] = c2;res[4] = c2;res[5] = c3;res[6] = c3;}}return res;}private:int get_diff(const string& color, char c1, char c2, char c3){int r_diff = get_hex(color[1], color[2]) - get_hex(c1, c1);int g_diff = get_hex(color[3], color[4]) - get_hex(c2, c2);int b_diff = get_hex(color[5], color[6]) - get_hex(c3, c3);return r_diff * r_diff + g_diff * g_diff + b_diff * b_diff;}int get_hex(char x, char y){return get_hex(x) * 16 + get_hex(y);}int get_hex(char x){if(x >= '0' && x <= '9')return x - '0';assert(x >= 'a' && x <= 'f');return 10 + (x - 'a');}};int main() {cout << Solution().similarRGB("#09f166") << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/similar-rgb-color/description//// Author : liuyubobobo/// Time : 2018-03-17#include <iostream>#include <cassert>#include <sstream>#include <iomanip>using namespace std;/// Brute Force/// Using std to handle hex based number and string////// Time Complexity: O(16*16*16*6)/// Space Complexity: O(1)class Solution {public:string similarRGB(string color) {int color_r = stoi(color.substr(1, 2), 0, 16);int color_g = stoi(color.substr(3, 2), 0, 16);int color_b = stoi(color.substr(5, 2), 0, 16);string res = "";int best = INT_MAX;for(int r = 0 ; r < 16 ; r ++)for(int g = 0 ; g < 16 ; g ++)for(int b = 0 ; b < 16 ; b ++){int diff = get_diff(color_r, color_g, color_b, r * 16 + r, g * 16 + g, b * 16 + b);if(diff < best){best = diff;res = get_hex_string(r * 16 + r, g * 16 + g, b * 16 + b);}}return res;}private:int get_diff(int r1, int g1, int b1, int r2, int g2, int b2){int r_diff = (r1 - r2) * (r1 - r2);int g_diff = (g1 - g2) * (g1 - g2);int b_diff = (b1 - b2) * (b1 - b2);return r_diff + g_diff + b_diff;}string get_hex_string(int r, int g, int b){stringstream ss("#");ss << std::hex << setfill('0') << setw(2);ss << r;ss << std::hex << setfill('0') << setw(2);ss << g;ss << std::hex << setfill('0') << setw(2);ss << b;return "#" + ss.str();}};int main() {cout << Solution().similarRGB("#09f166") << endl;cout << Solution().similarRGB("#1c9e03") << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/similar-rgb-color/description//// Author : liuyubobobo/// Time : 2018-03-17#include <iostream>#include <cassert>#include <sstream>#include <iomanip>using namespace std;/// Brute Force/// Dealing with each color component seperately////// Time Complexity: O(1)/// Space Complexity: O(1)class Solution {public:string similarRGB(string color) {int r = stoi(color.substr(1, 2), 0, 16);int g = stoi(color.substr(3, 2), 0, 16);int b = stoi(color.substr(5, 2), 0, 16);return "#" + f(r) + f(g) + f(b);}private:string f(int color){int res = color / 17 + (color % 17 > 8 ? 1 : 0);stringstream ss("#");ss << std::hex << setfill('0') << setw(2);ss << res * 17;return ss.str();}};int main() {cout << Solution().similarRGB("#09f166") << endl;cout << Solution().similarRGB("#1c9e03") << endl;return 0;}