{"id":1543,"date":"2018-01-07T21:58:42","date_gmt":"2018-01-08T05:58:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1543"},"modified":"2018-08-17T23:07:35","modified_gmt":"2018-08-18T06:07:35","slug":"leetcode-264-ugly-number-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-264-ugly-number-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 264. Ugly Number II"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8ba9\u4f60\u627e\u5230\u7b2cn\u4e2a<a href=\"http:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-263-ugly-number\/\">\u4e11\u6570<\/a>\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Write a program to find the\u00a0<code>n<\/code>-th ugly number.<\/p>\n<p>Ugly numbers are positive numbers whose prime factors only include\u00a0<code>2, 3, 5<\/code>. For example,\u00a0<code>1, 2, 3, 4, 5, 6, 8, 9, 10, 12<\/code>\u00a0is the sequence of the first\u00a0<code>10<\/code>\u00a0ugly numbers.<\/p>\n<p>Note that\u00a0<code>1<\/code>\u00a0is typically treated as an ugly number, and\u00a0<i>n<\/i>\u00a0<b>does not exceed 1690<\/b>.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1551\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/264-ep153.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/264-ep153.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/264-ep153-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/264-ep153-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n    int nthUglyNumber(int n) {\r\n        static vector&lt;int&gt; nums{1};\r\n        static int i2 = 0;\r\n        static int i3 = 0;\r\n        static int i5 = 0;\r\n        while (nums.size() &lt; n) {\r\n            const int next2 = nums[i2] * 2;\r\n            const int next3 = nums[i3] * 3;\r\n            const int next5 = nums[i5] * 5;\r\n            const int next = min(next2, min(next3, next5));\r\n            if (next == next2) ++i2;\r\n            if (next == next3) ++i3;\r\n            if (next == next5) ++i5;\r\n            nums.push_back(next);\r\n        }\r\n        return nums[n - 1];\r\n    }\r\n};<\/pre>\n<p>C++ \/query * O(NlogN)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 89 ms\r\nclass Solution {\r\npublic:\r\n    int nthUglyNumber(int n) {\r\n        vector&lt;int&gt; nums;\r\n        for (long a = 1; a &lt;= INT_MAX; a *= 2)\r\n            for (long b = a; b &lt;= INT_MAX; b *= 3)\r\n                for (long c = b; c &lt;= INT_MAX; c *= 5)\r\n                    nums.push_back(c);\r\n        std::sort(nums.begin(), nums.end());        \r\n        return nums[n - 1];\r\n    }\r\n};<\/pre>\n<p>C++ \/ static variables O(NlogN) + query*O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 7 ms\r\nclass Solution {\r\npublic:\r\n    int nthUglyNumber(int n) {\r\n        static vector&lt;int&gt; nums;\r\n        if (nums.empty()) {\r\n            for (long a = 1; a &lt;= INT_MAX; a *= 2)\r\n                for (long b = a; b &lt;= INT_MAX; b *= 3)\r\n                    for (long c = b; c &lt;= INT_MAX; c *= 5)\r\n                        nums.push_back(c);\r\n            std::sort(nums.begin(), nums.end());\r\n        }\r\n        return nums[n - 1];\r\n    }\r\n};<\/pre>\n<p>C++ \/ table O(1)<br \/>\n<a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/leetcode_264_table.cc\">leetcode_264_table.cc<\/a><\/p>\n<h1><strong>Related Problems:<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-263-ugly-number\/\">\u82b1\u82b1\u9171 LeetCode 263. Ugly Number<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8ba9\u4f60\u627e\u5230\u7b2cn\u4e2a\u4e11\u6570\u3002 Problem: Write a program to find the\u00a0n-th ugly number. Ugly numbers are positive numbers whose prime factors only include\u00a02, 3, 5. For example,\u00a01, 2,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[209],"class_list":["post-1543","post","type-post","status-publish","format-standard","hentry","category-math","tag-ugly-number","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1543","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=1543"}],"version-history":[{"count":12,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions"}],"predecessor-version":[{"id":3603,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions\/3603"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}