{"id":5890,"date":"2019-11-30T20:18:40","date_gmt":"2019-12-01T04:18:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5890"},"modified":"2019-11-30T20:19:34","modified_gmt":"2019-12-01T04:19:34","slug":"leetcode-1271-hexspeak","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1271-hexspeak\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1271. Hexspeak"},"content":{"rendered":"\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = \"257\"\n<strong>Output:<\/strong> \"IOI\"\n<strong>Explanation: <\/strong> 257 is 101 in hexadecimal.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = \"3\"\n<strong>Output:<\/strong> \"ERROR\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(logn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string toHexspeak(string num) {\n    long n = stol(num);\n    string ans;\n    while (n) {\n      int r = n % 16;\n      char c;\n      if (r == 0) c = 'O';\n      else if (r == 1) c = 'I';\n      else if (r >= 10 && r <= 15) c = 'A' + r - 10;\n      else return \"ERROR\";\n      ans += c;\n      n >>= 4;\n    }\n    reverse(begin(ans), end(ans));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A decimal number can be converted to its&nbsp;Hexspeak representation&nbsp;by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit&nbsp;0&nbsp;with the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[93,222,10,4],"class_list":["post-5890","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-conversion","tag-easy","tag-hex","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5890"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5890\/revisions"}],"predecessor-version":[{"id":5892,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5890\/revisions\/5892"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}