{"id":3571,"date":"2018-08-17T09:03:08","date_gmt":"2018-08-17T16:03:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3571"},"modified":"2018-08-17T09:05:49","modified_gmt":"2018-08-17T16:05:49","slug":"leetcode-504-base-7","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-504-base-7\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 504. Base 7"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 504. Base 7 - \u5237\u9898\u627e\u5de5\u4f5c EP12\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/fX6fh9B62vE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1>Problem<\/h1>\n<p>Given an integer, return its base 7 string representation.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> 100\r\n<b>Output:<\/b> \"202\"\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> -7\r\n<b>Output:<\/b> \"-10\"\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0The input will be in range of [-1e7, 1e7].<\/p>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Time complexity: O(logn)<\/p>\n<p>Space complexity: O(logn)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  string convertToBase7(int num) {\r\n    bool is_nagetive = num&lt;0;\r\n    num = abs(num);\r\n    string ans;\r\n    do {\r\n        ans += ('0' + (num % 7));\r\n        num \/= 7;\r\n    } while (num &gt; 0);\r\n    \r\n    if (is_nagetive)\r\n        ans += \"-\";\r\n    \r\n    reverse(begin(ans), end(ans));\r\n    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: &#8220;202&#8221; Example 2: Input: -7 Output: &#8220;-10&#8221; Note:\u00a0The input will&#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":[379,93,179,4],"class_list":["post-3571","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-base","tag-conversion","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3571","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=3571"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3571\/revisions"}],"predecessor-version":[{"id":3575,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3571\/revisions\/3575"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}