{"id":5926,"date":"2019-12-07T10:25:09","date_gmt":"2019-12-07T18:25:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5926"},"modified":"2019-12-07T10:26:23","modified_gmt":"2019-12-07T18:26:23","slug":"leetcode-405-convert-a-number-to-hexadecimal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-405-convert-a-number-to-hexadecimal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 405. Convert a Number to Hexadecimal"},"content":{"rendered":"\n<p>Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two\u2019s complement method is used.<\/p>\n\n\n\n<p>Note:<\/p>\n\n\n\n<p>All letters in hexadecimal (a-f) must be in lowercase.<br>\nThe 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>\nThe given number is guaranteed to fit within the range of a 32-bit signed integer.<br>\nYou must not use any method provided by the library which converts\/formats the number to hex directly.<br>\nExample 1:<\/p>\n\n\n\n<p>Input:<br>\n26<\/p>\n\n\n\n<p>Output:<br>\n&#8220;1a&#8221;<br>\nExample 2:<\/p>\n\n\n\n<p>Input:<br>\n-1<\/p>\n\n\n\n<p>Output:<br> &#8220;ffffffff&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<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>\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\nconstexpr char kHex[] = \"0123456789abcdef\";\n\nclass Solution {\npublic:\n  string toHex(int num) {\n    if (num == 0) return \"0\";\n    long t = num < 0 ? (1LL << 32) + num : num;\n    string ans;    \n    while (t) {\n      ans = kHex[t % 16] + ans;\n      t \/= 16;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two\u2019s complement method is used. Note: All letters in hexadecimal (a-f)&#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":[222,10,179],"class_list":["post-5926","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-hex","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5926","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=5926"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5926\/revisions"}],"predecessor-version":[{"id":5928,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5926\/revisions\/5928"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}