<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ugly number Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ugly-number/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ugly-number/</link>
	<description></description>
	<lastBuildDate>Sat, 18 Aug 2018 06:07:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>ugly number Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ugly-number/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 264. Ugly Number II</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-264-ugly-number-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-264-ugly-number-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 08 Jan 2018 05:58:42 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[ugly number]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1543</guid>

					<description><![CDATA[<p>题目大意：让你找到第n个丑数。 Problem: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-264-ugly-number-ii/">花花酱 LeetCode 264. Ugly Number II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>题目大意：让你找到第n个<a href="http://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/">丑数</a>。</p>
<p><strong>Problem:</strong></p>
<p>Write a program to find the <code>n</code>-th ugly number.</p>
<p>Ugly numbers are positive numbers whose prime factors only include <code>2, 3, 5</code>. For example, <code>1, 2, 3, 4, 5, 6, 8, 9, 10, 12</code> is the sequence of the first <code>10</code> ugly numbers.</p>
<p>Note that <code>1</code> is typically treated as an ugly number, and <i>n</i> <b>does not exceed 1690</b>.</p>
<p>&nbsp;</p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Solution:</strong></p>
<p>C++ / O(n)</p><pre class="crayon-plain-tag">// Author: huahua
// Running time: 6 ms
class Solution {
public:
    int nthUglyNumber(int n) {
        static vector&lt;int&gt; nums{1};
        static int i2 = 0;
        static int i3 = 0;
        static int i5 = 0;
        while (nums.size() &lt; n) {
            const int next2 = nums[i2] * 2;
            const int next3 = nums[i3] * 3;
            const int next5 = nums[i5] * 5;
            const int next = min(next2, min(next3, next5));
            if (next == next2) ++i2;
            if (next == next3) ++i3;
            if (next == next5) ++i5;
            nums.push_back(next);
        }
        return nums[n - 1];
    }
};</pre><p>C++ /query * O(NlogN)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 89 ms
class Solution {
public:
    int nthUglyNumber(int n) {
        vector&lt;int&gt; nums;
        for (long a = 1; a &lt;= INT_MAX; a *= 2)
            for (long b = a; b &lt;= INT_MAX; b *= 3)
                for (long c = b; c &lt;= INT_MAX; c *= 5)
                    nums.push_back(c);
        std::sort(nums.begin(), nums.end());        
        return nums[n - 1];
    }
};</pre><p>C++ / static variables O(NlogN) + query*O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 7 ms
class Solution {
public:
    int nthUglyNumber(int n) {
        static vector&lt;int&gt; nums;
        if (nums.empty()) {
            for (long a = 1; a &lt;= INT_MAX; a *= 2)
                for (long b = a; b &lt;= INT_MAX; b *= 3)
                    for (long c = b; c &lt;= INT_MAX; c *= 5)
                        nums.push_back(c);
            std::sort(nums.begin(), nums.end());
        }
        return nums[n - 1];
    }
};</pre><p>C++ / table O(1)<br />
<a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/leetcode_264_table.cc">leetcode_264_table.cc</a></p>
<h1><strong>Related Problems:</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/">花花酱 LeetCode 263. Ugly Number</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-264-ugly-number-ii/">花花酱 LeetCode 264. Ugly Number II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-264-ugly-number-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 263. Ugly Number</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 08 Jan 2018 04:58:39 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[ugly number]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1537</guid>

					<description><![CDATA[<p>题目大意：如果一个数的素数因子只有2，3，5，我们称它为丑数。让你判断一个数是不是丑数。 Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/">花花酱 LeetCode 263. Ugly Number</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/1wpe7yeqZd0?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：如果一个数的素数因子只有2，3，5，我们称它为丑数。让你判断一个数是不是丑数。</p>
<p><strong>Problem:</strong></p>
<p>Write a program to check whether a given number is an ugly number.</p>
<p>Ugly numbers are positive numbers whose prime factors only include <code>2, 3, 5</code>. For example, <code>6, 8</code> are ugly while <code>14</code> is not ugly since it includes another prime factor <code>7</code>.</p>
<p>Note that <code>1</code> is typically treated as an ugly number.</p>
<p>&nbsp;</p>
<p><strong>Idea:</strong></p>
<p>Math</p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 9 ms
class Solution {
public:
    bool isUgly(int num) {
        const vector&lt;int&gt; factors{2, 3, 5};
        for (const int factor : factors)
            while (num &amp;&amp; num % factor == 0) num /= factor;
        return num == 1;
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li>花花酱 <a href="http://zxi.mytechroad.com/blog/math/leetcode-264-ugly-number-ii/">LeetCode 264. Ugly Number II</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/">花花酱 LeetCode 263. Ugly Number</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-263-ugly-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
