<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rgb Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rgb/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rgb/</link>
	<description></description>
	<lastBuildDate>Sun, 18 Mar 2018 06:00:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>rgb Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rgb/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 800. Simple RGB Color</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-simple-rgb-color/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-simple-rgb-color/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Mar 2018 05:05:45 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[rgb]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2183</guid>

					<description><![CDATA[<p>Problem 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&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-simple-rgb-color/">花花酱 LeetCode 800. Simple RGB Color</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>In the following, every capital letter represents some hexadecimal digit from <code>0</code> to <code>f</code>.</p>
<p>The red-green-blue color <code>"#AABBCC"</code> can be written as <code>"#ABC"</code> in shorthand.  For example, <code>"#15c"</code> is shorthand for the color <code>"#1155cc"</code>.</p>
<p>Now, say the similarity between two colors <code>"#ABCDEF"</code> and <code>"#UVWXYZ"</code> is <code>-(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2</code>.</p>
<p>Given the color <code>"#ABCDEF"</code>, return a 7 character color that is most similar to <code>#ABCDEF</code>, and has a shorthand (that is, it can be represented as some <code>"#XYZ"</code></p>
<pre class="crayon:false "><strong>Example 1:</strong>
<strong>Input:</strong> color = "#09f166"
<strong>Output:</strong> "#11ee66"
<strong>Explanation: </strong> 
The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.
This is the highest among any shorthand color.
</pre>
<h2><strong>Note:</strong></h2>
<ul>
<li><code>color</code> is a string of length <code>7</code>.</li>
<li><code>color</code> is a valid RGB color: for <code>i &gt; 0</code>, <code>color[i]</code> is a hexadecimal digit from <code>0</code> to <code>f</code></li>
<li>Any answer which has the same (highest) similarity as the best answer will be accepted.</li>
<li>All inputs and outputs should use lowercase letters, and the output is 7 characters.</li>
</ul>
<h1><strong>Solution: Brute Force</strong></h1>
<p>R, G, B are independent, find the closest color for each channel separately.</p>
<p>Time complexity: O(3 * 16)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 7 ms
class Solution {
public:
  string similarRGB(string color) {
    const string hex{"0123456789abcdef"};
    vector&lt;int&gt; rgb(3, 0);
    for (int i = 0; i &lt; 3; ++i)
      rgb[i] = hex.find(color[2 * i + 1]) * 16 + hex.find(color[2 * i + 2]);
    
    string ans(7, '#');    
    for (int i = 0; i &lt; 3; ++i) {
      int best = INT_MAX;
      for (int j = 0; j &lt; 16; ++j) {
        int diff = abs(j * 16 + j - rgb[i]);
        if (diff &gt;= best) continue;
        best = diff;
        ans[2 * i + 1] = ans[2 * i + 2] = hex[j];
      }
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-simple-rgb-color/">花花酱 LeetCode 800. Simple RGB Color</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-simple-rgb-color/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
