leetcode800. Similar RGB Color(python)
原题地址:
题目
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.
Now after pouring some non-negative integer cups of champagne, return how full the j-th glass in the i-th row is (both i and j are 0 indexed.)
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.
代码
class Solution:
def similarRGB(self, color):
"""
:type color: str
:rtype: str
"""
a = ['0', '1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
string = '#'
for i in range(1,6,2):
if color[i] == color[i+1]:
string += color[i] + color[i]
elif a.index(color[i]) - a.index(color[i+1]) > 0:
if a.index(color[i]) - a.index(color[i+1]) > 8:
string += a[a.index(color[i])-1] + a[a.index(color[i])-1]
else:
string += color[i] + color[i]
else:
if a.index(color[i+1]) - a.index(color[i]) > 8:
string += a[a.index(color[i])+1] + a[a.index(color[i])+1]
else:
string += color[i] + color[i]
return string
版权声明:转载注明 http://blog.csdn.net/birdreamer/article/details/79600227