题目地址: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:

  1. color is a string of length 7.
  2. color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f
  3. 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++)的更多相关文章

  1. [LeetCode] 800. Similar RGB Color 相似的红绿蓝颜色

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

  2. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. BSA分析

    目录 两种算法 1. 欧氏距离(ED)算法 2. SNP-index算法 实操 1. 上游分析 2. 下游分析 两种算法 1. 欧氏距离(ED)算法 mut与wt分别代表突变型混池.野生型混池,A.C ...

  2. rabbitmq部署问题

    启动rabbitmq服务时报错: systemctl status rabbitmq-server 状态显示:Failed to start RabbitMQ broker Failed to sta ...

  3. urllib的基本使用介绍

    1. urllib中urlopen的基本使用介绍 1 ### urllib中urlopen的基本使用介绍 2 3 ## urlopen的基本用法(GET请求) 4 import urllib.requ ...

  4. Redis | 第9章 Lua 脚本与排序《Redis设计与实现》

    目录 前言 1. Lua 脚本 1.1 Redis 创建并修改 Lua 环境的步骤 1.2 Lua 环境协作组件 1.3 EVAL 命令的实现 1.4 EVALSHA 命令的实现 1.5 脚本管理命令 ...

  5. SpringCloud微服务实战——搭建企业级开发框架(三十):整合EasyExcel实现数据表格导入导出功能

      批量上传数据导入.数据统计分析导出,已经基本是系统必不可缺的一项功能,这里从性能和易用性方面考虑,集成EasyExcel.EasyExcel是一个基于Java的简单.省内存的读写Excel的开源项 ...

  6. C# / VB.NET 在Word中嵌入多媒体(视频、音频)文件

    Word中可将Office(Word/Excel/PowerPoint).PDF.txt等文件作为OLE对象插入到文档中,双击该对象可直接访问或编辑该文件,除了以上常见的文件格式对象,也可以插入多媒体 ...

  7. java运行报错 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    解决方法: 解决办法: 在项目的属性里设置jdk版本,方法是右击项目-->properties-->java compiler --> Enable project specific ...

  8. addict, address, adequate

    addict Addiction is a biopsychosocial disorder characterized by repeated use of drugs, or repetitive ...

  9. 数仓day03-----日志预处理

    1. 为什么要构建一个地理位置维表(字典) 在埋点日志中,有用户的地理位置信息,但是原始数据形式是GPS坐标,而GPS坐标在后续(地理位置维度分析)的分析中不好使用.gps坐标的匹配,不应该做这种精确 ...

  10. CentOS7 安装配置RocketMQ --主从模式(master-slave)异步复制

    机器信息 192.168.119.129 主 192.168.119.128 从 配置host[两台机器] vim /etc/hosts 添加 192.168.119.129 rocketmq-nam ...