<?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>hex Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hex/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hex/</link>
	<description></description>
	<lastBuildDate>Sat, 07 Dec 2019 18:26:23 +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>hex Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hex/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 405. Convert a Number to Hexadecimal</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-405-convert-a-number-to-hexadecimal/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-405-convert-a-number-to-hexadecimal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 07 Dec 2019 18:25:09 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5926</guid>

					<description><![CDATA[<p>Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. Note: All letters in hexadecimal (a-f)&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-405-convert-a-number-to-hexadecimal/">花花酱 LeetCode 405. Convert a Number to Hexadecimal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.</p>



<p>Note:</p>



<p>All letters in hexadecimal (a-f) must be in lowercase.<br>
The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character &#8216;0&#8217;; otherwise, the first character in the hexadecimal string will not be the zero character.<br>
The given number is guaranteed to fit within the range of a 32-bit signed integer.<br>
You must not use any method provided by the library which converts/formats the number to hex directly.<br>
Example 1:</p>



<p>Input:<br>
26</p>



<p>Output:<br>
&#8220;1a&#8221;<br>
Example 2:</p>



<p>Input:<br>
-1</p>



<p>Output:<br> &#8220;ffffffff&#8221;</p>



<h2><strong>Solution: Simulation</strong></h2>



<p>if input is negative, add 2^32 to it (e.g. set the highest bit to 1)<br>while num is non zero, mod it by 16 and prepend the remainder to ans string, then divide num by 16.</p>



<p>Time complexity: O(logn)<br>Space complexity: O(logn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
constexpr char kHex[] = &quot;0123456789abcdef&quot;;

class Solution {
public:
  string toHex(int num) {
    if (num == 0) return &quot;0&quot;;
    long t = num &lt; 0 ? (1LL &lt;&lt; 32) + num : num;
    string ans;    
    while (t) {
      ans = kHex[t % 16] + ans;
      t /= 16;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-405-convert-a-number-to-hexadecimal/">花花酱 LeetCode 405. Convert a Number to Hexadecimal</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/simulation/leetcode-405-convert-a-number-to-hexadecimal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1271. Hexspeak</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1271-hexspeak/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1271-hexspeak/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Dec 2019 04:18:40 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5890</guid>

					<description><![CDATA[<p>A decimal number can be converted to its&#160;Hexspeak representation&#160;by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit&#160;0&#160;with the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1271-hexspeak/">花花酱 LeetCode 1271. Hexspeak</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A decimal number can be converted to its&nbsp;<em>Hexspeak representation</em>&nbsp;by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit&nbsp;<code>0</code>&nbsp;with the letter&nbsp;<code>O</code>, and the digit&nbsp;<code>1</code>&nbsp;with the letter&nbsp;<code>I</code>.&nbsp; Such a representation&nbsp;is&nbsp;<em>valid</em>&nbsp;if and only if it consists only of the letters in the set&nbsp;<code>{"A", "B", "C", "D", "E", "F", "I", "O"}</code>.</p>



<p>Given a string&nbsp;<code>num</code>&nbsp;representing a decimal integer&nbsp;<code>N</code>, return the Hexspeak representation of&nbsp;<code>N</code>&nbsp;if it is valid, otherwise return&nbsp;<code>"ERROR"</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = "257"
<strong>Output:</strong> "IOI"
<strong>Explanation: </strong> 257 is 101 in hexadecimal.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = "3"
<strong>Output:</strong> "ERROR"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= N &lt;= 10^12</code></li><li>There are no leading zeros in the given string.</li><li>All answers must be in uppercase letters.</li></ul>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(logn)<br>Space complexity: O(logn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string toHexspeak(string num) {
    long n = stol(num);
    string ans;
    while (n) {
      int r = n % 16;
      char c;
      if (r == 0) c = 'O';
      else if (r == 1) c = 'I';
      else if (r &gt;= 10 &amp;&amp; r &lt;= 15) c = 'A' + r - 10;
      else return &quot;ERROR&quot;;
      ans += c;
      n &gt;&gt;= 4;
    }
    reverse(begin(ans), end(ans));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1271-hexspeak/">花花酱 LeetCode 1271. Hexspeak</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/simulation/leetcode-1271-hexspeak/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>[ZOJ] 3713: In 7-bit</title>
		<link>https://zxi.mytechroad.com/blog/zoj/zoj-3713-in-7-bit/</link>
					<comments>https://zxi.mytechroad.com/blog/zoj/zoj-3713-in-7-bit/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 13 Mar 2017 00:40:57 +0000</pubDate>
				<category><![CDATA[ZOJ]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[oj]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=14</guid>

					<description><![CDATA[<p>[crayon-663c6933ef1fd516504885/] &#160;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/zoj/zoj-3713-in-7-bit/">[ZOJ] 3713: In 7-bit</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">// 3934609  2017-03-13 08:38:54 Accepted  3713  C++0x 290 6428  xxfflower
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;iomanip&gt;
using namespace std;

void put_hex(int byte) {
   cout &lt;&lt; hex &lt;&lt; uppercase &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; byte;
}

void put_str(const string&amp; s) {
  for(char c : s)
    put_hex(c);
}

void put_len(int length) {
  while(length&gt;=128) {
    put_hex(length % 128 + 128);
    length &gt;&gt;= 7;
  }
  put_hex(length);
}

int main(int argc, char const *argv[])
{
  int n;
  cin&gt;&gt;n;
  string s;
  getline(cin, s);

  while(n--) {
    getline(cin, s);
    put_len(s.length());
    put_str(s);
    cout&lt;&lt;endl;
  }
  return 0;
}</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/zoj/zoj-3713-in-7-bit/">[ZOJ] 3713: In 7-bit</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/zoj/zoj-3713-in-7-bit/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
