# 800. Similar RGB Color

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/similar-rgb-color/description/

> 内容描述

```

In the following, every capital letter represents some hexadecimal digit from 0 to f.

The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand.  For example, "#15c" is shorthand for the color "#1155cc".

Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2.

Given the color "#ABCDEF", return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some "#XYZ"

Example 1:
Input: color = "#09f166"
Output: "#11ee66"
Explanation:  
The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.
This is the highest among any shorthand color.
Note:

color is a string of length 7.
color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f
Any answer which has the same (highest) similarity as the best answer will be accepted.
All inputs and outputs should use lowercase letters, and the output is 7 characters.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(1)******- 空间复杂度: O(1)******


暴力，我也是醉了，一道easy题居然被我写超时了。。。。。



```
class Solution(object):
    def similarRGB(self, color):
        """
        :type color: str
        :rtype: str
        """
        def getSimilarity(color1, color2):
            res = 0
            for i in range(1, 6, 2):
                res -= pow(int(color1[i:i+2], 16)-int(color2[i:i+2], 16), 2)
            return res
        res, max_num = '', -sys.maxsize
        for i in range(16):
            for j in range(16):
                for k in range(16):
                    new_color = '#' + ''.join(map(lambda x: hex(x)[2:] * 2, [i, j, k]))
                    if getSimilarity(color, new_color) > max_num:
                        max_num = getSimilarity(color, new_color)
                        res = new_color
        return res
```


> 思路 2
******- 时间复杂度: O(1)******- 空间复杂度: O(1)******

瞬间觉得是不是要位运算，发现不用位运算也可以

分成R，G，B三部分，每部分都取最小肯定最小

beats 79.55%

```python
class Solution(object):
    def similarRGB(self, color):
        """
        :type color: str
        :rtype: str
        """   
        def getClosest(s):
            return min(['00', '11', '22', '33', '44', '55', '66', '77', '88', '99', 'aa', 'bb', 'cc', 'dd', 'ee', 'ff'],
                key=lambda x: abs(int(s, 16) - int(x, 16)))

        res = [getClosest(color[i:i+2]) for i in range(1, len(color), 2)]
        return '#' + ''.join(res)
```
















