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 > 0color[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.

这道题定义了一种表示颜色的十六进制字符串,然后说是有一种两两字符相等的颜色可以缩写。然后又给了我们一个人一的字符串,让我们找出距离其最近的可以缩写的颜色串。题意不难理解,而且还是Easy标识符,所以我们要有信心可以将其拿下。那么通过分析题目中给的例子, 我们知道可以将给定的字符串拆成三个部分,每个部分分别来进行处理,比如对于字符串"#09f166"来说,我们就分别处理"09","f1","66"即可。我们的目标是要将每部分的两个字符变为相同,并且跟原来的距离最小,那么实际上我们并不需要遍历所有的组合,因为比较有参考价值的就是十位上的数字,因为如果十位上的数字不变,或者只是增减1,而让个位上的数字变动大一些,这样距离会最小,因为个位上的数字权重最小。就拿"09"来举例,这个数字可以变成"11"或者"00",十六进制数"11"对应的十进制数是17,跟"09"相差了8,而十六进制数"00"对应的十进制数是0,跟"09"相差了9,显然我们选择"11"会好一些。所以我们的临界点是"8",如果个位上的数字大于"8",那么十位上的数就加1。

下面来看如何确定十位上的数字,比如拿"e1"来举例,其十进制数为225,其可能的选择有"ff","ee",和"dd",其十进制数分别为255,238,和221,我们目测很容易看出来是跟"dd"离得最近,但是怎么确定十位上的数字呢。我们发现"11","22","33","44"... 这些数字之间相差了一个"11",十进制数为17,所以我们只要将原十六进制数除以一个"11",就知道其能到达的位置,比如"e1"除以"11",就只能到达"d",那么十进制上就是"d",至于个位数的处理情况跟上面一段讲解相同,我们对"11"取余,然后跟临界点"8"比较,如果个位上的数字大于"8",那么十位上的数就加1。这样就可以确定正确的数字了,那么组成正确的十六进制字符串即可,参见代码如下:

解法一:

class Solution {
public:
string similarRGB(string color) {
return "#" + helper(color.substr(, )) + helper(color.substr(, )) + helper(color.substr(, ));
}
string helper(string str) {
string dict = "0123456789abcdef";
int num = stoi(str, nullptr, );
int idx = num / + (num % > ? : );
return string(, dict[idx]);
}
};

我们也可以不用helper函数,直接在一个函数中搞定即可,参见代码如下:

解法二:

class Solution {
public:
string similarRGB(string color) {
for (int i = ; i < color.size(); i += ) {
int num = stoi(color.substr(i, ), nullptr, );
int idx = num / + (num % > ? : );
color[i] = color[i + ] = (idx > ) ? (idx - + 'a') : (idx + '');
}
return color;
}
};

参考资料:

https://leetcode.com/problems/similar-rgb-color/solution/

https://leetcode.com/problems/similar-rgb-color/discuss/120077/C++-Concise-Solution-with-explanation-6ms

https://leetcode.com/problems/similar-rgb-color/discuss/120093/C++-O(1)-time-and-space-Easy-to-Understand

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Similar RGB Color 相似的红绿蓝颜色的更多相关文章

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

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

  2. FPGA驱动LCD显示红绿蓝彩条

    实验目的:先简单熟悉LCD灯的驱动和时序图的代码实现.设计功能是让LCD显示红绿蓝三种颜色,即三个彩带.本次实验比较容易实现,主要是对LCD驱动时序图的理解和时序参数的配置. 实验条件:1.LCD原理 ...

  3. 【LeetCode】800. Similar RGB Color 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  4. Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调—Jaosn niu

    #!/usr/bin/python # coding:utf-8 from PIL import Image, ImageDraw from HierarchicalClustering import ...

  5. 阶段小项目1:循环间隔1秒lcd显示红绿蓝

    #include<stdlib.h>#include<stdio.h>#include<string.h>#include<error.h>#inclu ...

  6. C#Color对象的使用介绍及颜色对照表

    原文地址  http://blog.sina.com.cn/s/blog_3e1177090101bzs3.html 今天用到了特转载 NET框架中的颜色基于4种成份,透明度,红,绿和蓝.每一种成份都 ...

  7. RGB Color Codes Chart

    RGB Color Codes Chart RGB颜色空间 RGB颜色空间或RGB颜色系统,从红色.绿色和蓝色的组合中构造所有颜色. 红色.绿色和蓝色各使用8位,它们的整数值从0到255.这使得256 ...

  8. 【AGC025B】RGB Color

    [AGC025B]RGB Color 题面描述 Link to Atcoder Link to Luogu Takahashi has a tower which is divided into \( ...

  9. 保护眼睛,绿豆沙颜色的RGB值和HSL值

    现在的人尤其是职场中人,每天都得花很长时间对着电脑,对眼睛的伤害很大,其实我们可以对电脑进行一个简单的设置,把窗口背景设置成绿豆沙颜色的,对眼睛的保护很有帮助的. 下面是绿豆沙颜色的RGB值和HSL值 ...

随机推荐

  1. Node.js实战项目学习系列(2) 开发环境和调试工具

    前言 上一节让我们对Node.js有一个初步的了解,那么现在可以开始正式学习下Node.js的开发了,但是任何一门语言要设计到开发,就必须先学习开发环境以及调试.本文将主要讲解这些内容. 本文涉及到的 ...

  2. Beamer中左边画图, 右边文字解释

    \begin{columns} \column{.4\textwidth} \begin{figure} \centering % Requires \usepackage{graphicx} \in ...

  3. [物理学与PDEs]第1章第6节 电磁场的标势与矢势 6.1 预备知识

    1.  若 ${\bf B}$ 为横场 ($\Div{\bf B}=0\ra {\bf k}\cdot {\bf B}=0\ra $ 波的振动方向与传播方向平行), 则 $$\bex \exists\ ...

  4. burp suite 基础入门超详细教程

    介绍: 都是我个人了解到的信息,,分享给大家 欢迎指正 burp suite 被誉为web安全工具中的瑞士军刀. 大家知道,瑞士军刀,都是体积小,功能强悍,.西方军队的标配.说这么多,只是想强调这款工 ...

  5. SpringBoot实战——微信点餐系统

    1.初始化项目 引入模块 <dependencies> <dependency> <groupId>org.springframework.boot</gro ...

  6. 题解-AtCoder Code-Festival2017 Final-J Tree MST

    Problem \(\mathrm{Code~Festival~2017~Final~J}\) 题意概要:一棵 \(n\) 个节点有点权边权的树.构建一张完全图,对于任意一对点 \((x,y)\),连 ...

  7. JS基础之传参(值传递、对象传递)

    一.概念 我们需了解什么是按值传递(call by value),什么是按引用传递(call by reference).在计算机科学里,这个部分叫求值策略(Evaluation Strategy). ...

  8. 【算法】K最近邻算法(K-NEAREST NEIGHBOURS,KNN)

    K最近邻算法(k-nearest neighbours,KNN) 算法 对一个元素进行分类 查看它k个最近的邻居 在这些邻居中,哪个种类多,这个元素有更大概率是这个种类 使用 使用KNN来做两项基本工 ...

  9. 小程序引入百度api天气预报

    先看下最终的效果(默认可以获得未来三天数据): 第一:首先准备条件(必须): 1.小程序已认证,有appID 2.必须把https://api.map.baidu.com 添加到小程序的合法域名列表中 ...

  10. install mysql on centos7

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了 MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的 ...