forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
48 lines (35 loc) · 979 Bytes
/
main3.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// 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;
}