Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2,
3, 5
. For example, 6, 8 are ugly while 14 is
not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly
number.

分析:

题目说的非常清楚。所谓丑数,就是那些因子仅仅含2,3,5的数

那么依据丑陋数的定义,我们将给定数除以2、3、5,直到无法整除,也就是除以2、3、5的余数不再为0时停止

这时假设得到1,说明是全部因子都是2或3或5,假设不是1。则不是丑陋数。

class Solution {
public:
bool isUgly(int num) {
if(num<=0)
return false;
if(num==1)
return true; while(num>=2 && num%2==0) //先将因子2除完,接着3,5
num/=2;
while(num>=3 && num%3==0)
num/=3;
while(num>=5 && num%5==0)
num/=5; return num==1;
}
};

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, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly
numbers.

Note that 1 is typically treated as an ugly
number.

Hint:

  1. The naive approach is to call isUgly for
    every number until you reach the nth one. Most numbers are not ugly.
  2. Try to focus your effort on generating only the ugly ones.Show More
    Hint













分析:

提示说的非常清楚,第n个丑数的沿途大量的数都不是丑数。为了能跳过对他们的推断

直接由小到大生成丑数进行统计个数,直到是第n个为止

用三个暂时数和数组来渐进增大丑数。

本质上以下这样的解法是动态规划,显然第n个丑数肯定是由前面某个丑数乘以2或者3或者5,

问题就在于生成的丑数要是有序的!

这可就麻烦了,三个指针总是维护*2*3*5的结果,他们总是下一个候选丑数!

令figure2是由某个较小丑数*2产生的新丑数,

令figure3是由某个较小丑数*3产生的新丑数,

令figure5是由某个较小丑数*5产生的新丑数,

则第n个丑数肯定是三者中的较小者。可是必须得保证我们三者的生成规则。

三者的生成规则:

显然下一次的figure2。figure3。figure5必须比当前产生的丑数大。

那么就要求一旦当前产生的丑数大比三个候选丑数大就将其变大一点。从小到大沿途乘以2/3/5(详细看代码)

下面设计參考别人:

//首先思路:提示说的非常清楚,第n个丑数的沿途大量的数都不是丑数,为了能跳过对他们的推断
//直接由小到大生成丑数进行统计个数。直到是第n个为止
//用三个暂时数来渐进增大的丑数
class Solution {
public:
int nthUglyNumber(int n)
{
vector<int> ugly(n,0);
ugly[0]=1;
int figure2=2,figure3=3,figure5=5;
int index2=0,index3=0,index5=0;
for(int i=1;i<n;++i)
{
ugly[i]=min(figure2,min(figure3,figure5));//依据数列得到最小的值
if(figure2<=ugly[i])
figure2=2*ugly[++index2];
if(figure3<=ugly[i])
figure3=3*ugly[++index3];
if(figure5<=ugly[i])
figure5=5*ugly[++index5]; }
return ugly[n-1];
}
};

其它小伙伴的做法:

全部的ugly number都是由1開始。渐乘2/3/5一步一步生成。

仅仅要将这些生成的数排序就可以获得,自己主动排序能够使用set集

这样每次取出的第一个元素就是最小元素,由此再继续生成新的ugly number.

这样做有一个弊端,每次插入都有O(lg(N))的时间复杂度,所以比上一种方法慢。

class Solution {
public:
int nthUglyNumber(int n) {
set<long long> order;//最小堆(顶是最小值)同意反复数据存在。不能使用堆
order.insert(1);
int count = 0;
long long curmin = 0;
while(count < n)
{
curmin = *(order.begin());
order.erase(order.begin());
order.insert(curmin * 2);
order.insert(curmin * 3);
order.insert(curmin * 5);
count ++;
}
return (int)curmin;
}
};

Write a program to find the nth super ugly number.

Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of
size k. For example, [1,
2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]
is the sequence of the first 12 super ugly numbers given primes = [2,
7, 13, 19]
 of size 4.

Note:

(1) 1 is a super ugly number for any given primes.

(2) The given numbers in primes are in ascending order.

(3) 0 < k ≤ 100, 0 < n ≤
106, 0 < primes[i] <
1000.

分析:

方法和上面一题一样,仅仅只是因子多了几个而已。变量都取得一样。就不多解释了!

class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> ugly(n,0);
vector<int> figures(primes);
vector<int> indexs(n,0);
ugly[0]=1;
for(int i=1;i<n;i++)
{
ugly[i]=figures[0];
for(int j=1;j<primes.size();j++)
ugly[i]=min(ugly[i],figures[j]);
for(int j=0;j<primes.size();j++)
if(figures[j]<=ugly[i])
figures[j]=ugly[++indexs[j]]*primes[j];
}
return ugly[n-1];
}
};

參考资源:

【1】网友。tyq101010。原文地址, http://blog.csdn.net/tyq101010/article/details/49256889

注:本博文为EbowTang原创。兴许可能继续更新本文。

假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/51581228

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

&lt;LeetCode OJ&gt; 26 / 264 / 313 Ugly Number (I / II / III)的更多相关文章

  1. &lt;LeetCode OJ&gt; 141 / 142 Linked List Cycle(I / II)

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  2. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

  3. Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)

    Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...

  4. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...

  5. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  6. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  7. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  8. LeetCode OJ 之 Ugly Number (丑数)

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. [CF864F]Cities Excursions

    题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...

  2. 【R笔记】R语言进阶之4:数据整形(reshape)

    R语言进阶之4:数据整形(reshape) 2013-05-31 10:15 xxx 网易博客 字号:T | T 从不同途径得到的数据的组织方式是多种多样的,很多数据都要经过整理才能进行有效的分析,数 ...

  3. no such file or directory : 'users/shikx/xxx/xxx/Appirater.m'

    删除此处红色的.m文件即可

  4. 简单说说DNS劫持_firefox吧_百度贴吧

    简单说说DNS劫持_firefox吧_百度贴吧 DNSSEC

  5. Qemu 有用的链接

    Qemu下载和编译 Download https://en.wikibooks.org/wiki/QEMU/Linux https://en.wikibooks.org/wiki/QEMU/Insta ...

  6. easyui dialog 按钮动态命名

    1.方法一: /** * grid新增 * 弹框并且获取支付类型 */ function gridAdd() { var dlg = $('#mydialog').dialog({ title : & ...

  7. Mounting the NFS share on a Windows server

    今天遇到一个相当奇怪的问题,在windows 上mount LINUX NFS, powershell 脚本可以成功, 用图形界面也可以成功,但BATCH就是不行.提示53网络错误. 不过公司已经有人 ...

  8. 开源 免费 java CMS - FreeCMS1.9 移动APP管理 栏目配置

    项目地址:http://www.freeteam.cn/ 栏目配置 管理员能够在这里设设置栏目是否是否同意移动app訪问.栏目页的布局等属性. 从左側管理菜单点击栏目配置进入. 选择须要管理的栏目后点 ...

  9. B7:访问者模式 Visitor

    表示一个作用于某对象结构中各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 适用场景:1.适用于元素类数据结构相对稳定(类的方法固定,但属性可以变化,如果方法变化很大,就无 ...

  10. 利用问答机器人API开发制作聊天类App

    缘起 很久没写项目了,所以单纯的想练练手,正好看到有问答机器人的接口,想到之前也做过聊天项目,为什么不实验一下呢.当然也是简单调用接口的项目,并没有真正的完成问答的算法等等.业余项目,功能不齐全,只实 ...