{"id":1537,"date":"2018-01-07T20:58:39","date_gmt":"2018-01-08T04:58:39","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1537"},"modified":"2018-01-08T08:55:52","modified_gmt":"2018-01-08T16:55:52","slug":"leetcode-263-ugly-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-263-ugly-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 263. Ugly Number"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 263 Ugly Number - \u5237\u9898\u627e\u5de5\u4f5c EP152\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/1wpe7yeqZd0?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<p>\u9898\u76ee\u5927\u610f\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u7684\u7d20\u6570\u56e0\u5b50\u53ea\u67092\uff0c3\uff0c5\uff0c\u6211\u4eec\u79f0\u5b83\u4e3a\u4e11\u6570\u3002\u8ba9\u4f60\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u4e0d\u662f\u4e11\u6570\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Write a program to check whether a given number is an 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>6, 8<\/code>\u00a0are ugly while\u00a0<code>14<\/code>\u00a0is not ugly since it includes another prime factor\u00a0<code>7<\/code>.<\/p>\n<p>Note that\u00a0<code>1<\/code>\u00a0is typically treated as an ugly number.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Math<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1556\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/263-ep152.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/263-ep152.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/263-ep152-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/263-ep152-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n    bool isUgly(int num) {\r\n        const vector&lt;int&gt; factors{2, 3, 5};\r\n        for (const int factor : factors)\r\n            while (num &amp;&amp; num % factor == 0) num \/= factor;\r\n        return num == 1;\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li>\u82b1\u82b1\u9171 <a href=\"http:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-264-ugly-number-ii\/\">LeetCode 264. Ugly Number II<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u7684\u7d20\u6570\u56e0\u5b50\u53ea\u67092\uff0c3\uff0c5\uff0c\u6211\u4eec\u79f0\u5b83\u4e3a\u4e11\u6570\u3002\u8ba9\u4f60\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u4e0d\u662f\u4e11\u6570\u3002 Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include\u00a02,&#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-1537","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\/1537","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=1537"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1537\/revisions"}],"predecessor-version":[{"id":1558,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1537\/revisions\/1558"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}