{"id":7272,"date":"2020-08-22T11:18:35","date_gmt":"2020-08-22T18:18:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7272"},"modified":"2020-08-22T11:19:01","modified_gmt":"2020-08-22T18:19:01","slug":"leetcode-1556-thousand-separator","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1556-thousand-separator\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1556. Thousand Separator"},"content":{"rendered":"\n<p>Given an&nbsp;integer&nbsp;<code>n<\/code>, add a dot (&#8220;.&#8221;)&nbsp;as the thousands separator and return it in&nbsp;string format.<\/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> n = 987\n<strong>Output:<\/strong> \"987\"\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> n = 1234\n<strong>Output:<\/strong> \"1.234\"\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 123456789\n<strong>Output:<\/strong> \"123.456.789\"\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 0\n<strong>Output:<\/strong> \"0\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= n &lt; 2^31<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Digit by digit<\/strong><\/p>\n\n\n\n<p>Time complexity: O(log^2(n)) -> O(logn)<br>Space complexity: O(log(n))<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\nclass Solution {\npublic:\n  string thousandSeparator(int n) {    \n    string ans;\n    int count = 0;\n    do {\n      if (count++ % 3 == 0 && ans.size())\n        ans = \".\" + ans;\n      ans = to_string(n % 10) + ans;\n      n \/= 10;      \n    } while (n);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an&nbsp;integer&nbsp;n, add a dot (&#8220;.&#8221;)&nbsp;as the thousands separator and return it in&nbsp;string format. Example 1: Input: n = 987 Output: &#8220;987&#8221; Example 2: Input:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,276,4],"class_list":["post-7272","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-split","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7272","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=7272"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7272\/revisions"}],"predecessor-version":[{"id":7274,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7272\/revisions\/7274"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}