{"id":5009,"date":"2019-04-03T22:11:17","date_gmt":"2019-04-04T05:11:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5009"},"modified":"2019-04-03T22:20:07","modified_gmt":"2019-04-04T05:20:07","slug":"leetcode-273-integer-to-english-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-273-integer-to-english-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 273. Integer to English Words"},"content":{"rendered":"\n<p>Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2<sup>31<\/sup>&nbsp;&#8211; 1.<\/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> 123\n<strong>Output:<\/strong> \"One Hundred Twenty Three\"\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> 12345\n<strong>Output:<\/strong> \"Twelve Thousand Three Hundred Forty Five\"<\/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> 1234567\n<strong>Output:<\/strong> \"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven\"\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> 1234567891\n<strong>Output:<\/strong> \"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One\"<\/pre>\n\n\n\n<p><strong>Solution: Recursion<\/strong><\/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 * kUnder20[] = {\"One\", \"Two\", \"Three\", \"Four\", \"Five\", \n                               \"Six\", \"Seven\", \"Eight\", \"Nine\",\"Ten\",\n                               \"Eleven\", \"Twelve\", \"Thirteen\", \"Fourteen\",\n                               \"Fifteen\", \"Sixteen\", \"Seventeen\", \"Eighteen\", \n                               \"Nineteen\"};\nconstexpr char * kUnder100[] = {\"Twenty\", \"Thirty\", \"Forty\", \"Fifty\", \n                                \"Sixty\", \"Seventy\", \"Eighty\", \"Ninety\"};\nconstexpr char * kHTMB[] = {\"Hundred\", \"Thousand\", \"Million\", \"Billion\"};\nconstexpr long kP[] = {100, 1000, 1000*1000, 1000*1000*1000};\n\nclass Solution {\npublic:\n  string numberToWords(int n) {\n    if (n == 0) return \"Zero\";\n    return convert(n).substr(1);\n  }\nprivate:    \n  string convert(int n) {\n    if (n == 0) return \"\";\n    if (n < 20) return string(\" \") + kUnder20[n - 1]; \/\/ 1 - 19\n    if (n < 100) return string(\" \") + kUnder100[n \/ 10 - 2] + convert(n % 10); \/\/ 20 ~ 99\n    for (int i = 3; i >= 0; --i)\n      if (n >= kP[i]) return convert(n \/ kP[i]) + \" \" + kHTMB[i] + convert(n % kP[i]);\n    return \"\";\n  }\n};\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231&nbsp;&#8211; 1. Example 1: Input: 123 Output: &#8220;One&#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":[17],"class_list":["post-5009","post","type-post","status-publish","format-standard","hentry","category-string","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5009","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=5009"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5009\/revisions"}],"predecessor-version":[{"id":5014,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5009\/revisions\/5014"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}