【LeetCode】800. Similar RGB Color 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/similar-rgb-color/
题目描述
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.
题目大意
RGB 颜色用十六进制来表示的话,每个大写字母都代表了某个从 0 到 f 的 16 进制数。
RGB 颜色 “#AABBCC” 可以简写成 “#ABC” 。例如,"#15c" 其实是 “#1155cc” 的简写。
现在,假如我们分别定义两个颜色 “#ABCDEF” 和 “#UVWXYZ”,则他们的相似度可以通过这个表达式 -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2 来计算。
那么给定颜色 “#ABCDEF”,请你返回一个与 #ABCDEF 最相似的 7 个字符代表的颜色,并且它是可以被简写形式表达的。(比如,可以表示成类似 “#XYZ” 的形式)
解题方法
遍历
这个题的解法写的比较长,首先写了个十六进制和十进制转换函数,然后对R、G、B三个位置进行排列组合,看哪个组合和给出的color最相似。
需要注意的是这个相似函数是负数,因此最相似的时候是相似度最大。
C++代码如下:
class Solution {
public:
string similarRGB(string color) {
vector<string> hexs = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f"};
int R = hex2dec(color.substr(1, 2));
int G = hex2dec(color.substr(3, 2));
int B = hex2dec(color.substr(5, 2));
int mostSimilar = INT_MIN;
string res;
for (int i = 0; i < hexs.size(); ++i) {
for (int j = 0; j < hexs.size(); ++j) {
for (int k = 0; k < hexs.size(); ++k) {
string newR = hexs[i] + hexs[i];
string newG = hexs[j] + hexs[j];
string newB = hexs[k] + hexs[k];
int intR = hex2dec(newR);
int intG = hex2dec(newG);
int intB = hex2dec(newB);
int curSimilar = - (R - intR) * (R - intR) - (G - intG) * (G - intG) - (B - intB) * (B - intB);
if (curSimilar > mostSimilar) {
res = "#" + newR + newG + newB;
mostSimilar = curSimilar;
}
}
}
}
return res;
}
int hex2dec(string hex) {
int res = 0;
for (int i = 0; i < hex.size(); ++i) {
if (hex[i] >= 'a') {
res = 16 * res + (hex[i] - 'a' + 10);
} else {
res = 16 * res + (hex[i] - '0');
}
}
return res;
}
};
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】800. Similar RGB Color 解题报告(C++)的更多相关文章
- [LeetCode] 800. Similar RGB Color 相似的红绿蓝颜色
In the following, every capital letter represents some hexadecimal digit from 0 to f. The red-green- ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- awk多文件处理时的分隔符?
目录 问题来源 解决方法 问题来源 我有两个文件,一个是plink过滤后得到的.fam文件(空格分隔):另一个是样本对应关系文件(tab分隔). 文件1: 文件2: 两个文件匹配,awk常规操作.这里 ...
- 【5】肿瘤DNA甲基化数据分析原理及流程
目录 导论 DNA甲基化基本概论 检测DNA甲基化的方法 DNA甲基化数据分析流程及方法 DNA甲基化在肿瘤研究中的应用 导论 表观遗传:非DNA决定的基因表达,或表型改变中可遗传因素的研究 DNA水 ...
- No.1 R语言在生物信息中的应用——序列读取及格式化输出
目的:读入序列文件(fasta格式),返回一个数据框,内容包括--存储ID.注释行(anno).长度(len).序列内容(content) 一.问题思考: 1. 如何识别注释行和序列内容行 2. 如何 ...
- 工作学习2-gcc升级引发的崩溃
分享一下调查gcc 8.0下,函数漏写返回值崩溃问题,调查记录. 现在新的硬件,基本操作系统都是redhat 8.0,升级后测试时,发现了一个崩溃问题,记录一下. ================== ...
- 学习java的第二十天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- github小白的记录随笔
此文章是基础本地安装好了git环境的新手小白. 进入您要上传项目的根路径,右键选择Git Bash Here. 输入命令: git init //初始化git仓库环境 git remote add o ...
- A Child's History of England.45
To forgive these unworthy princes was only to afford them breathing-time for new faithlessness. They ...
- android:textAppearance解析
Android的系统自带的文字外观设置及实际显示效果图 android:textAppearancexml布局里面设置文字的外观: 如"android:textAppearance=&quo ...
- Javaj基础知识runtime error
遇到的java 运行时错误: NullPointerException空指针 ,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始 ...
- 小飞机可以解决git clone没有返回的问题吗?
[1]Linux如何使用小飞机? 以ss为例,先下载客户端: https://www.mediafire.com/folder/xag0zy318a5tt/Linux 下载客户端以后,右键把权限中&q ...