Ugly Number

Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy

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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

class Solution {
public:
bool isUgly(int num) {
if (num == || num == || num == || num == ) return true;
vector<int> u = {,,};
for (int i = ; i < u.size(); i++) {
if (num % u[i] == ) {
int p = num / u[i];
if (p == || p == || p == || !isPrime(p) && isUgly(p)) return true;
}
}
return false;
} bool isPrime(int num) {
for (int i = ; i*i <= num; i++) {
if (num % i == ) return false;
}
return true;
}
};

这题花了一番功夫,主要卡在质因数分解上。我的思路是用2,3,5去整除num,如果结果不是2,3,5而且不是其他任何质数的话,它就是ugly的。但是这个思路有个问题,以28为例子,用2除的得14,因此14不是2,3,5中的一个同时也不是质数,所以被判为了ugly。但事实是14还可以被分解为2 x 7, 而7 是质数,因此7 也是28的一个质因子,因此28不是ugly数,怎么办呢,灵机一抖的我想到如果再保证除2,3,5以后的数是ugly的不就解决了。。。所以我在超长的if 语句后面加上isUgly的递归调用,结果证明这个思路是正确的 Accepted.

[LeetCode] Ugly Number的更多相关文章

  1. 力不从心 Leetcode(ugly number heap) 263, 264,313

    Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...

  2. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  3. [LeetCode] Ugly Number 丑陋数

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

  4. [LeetCode] Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  5. [LeetCode] Ugly Number II (A New Question Added Today)

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. [LeetCode] Ugly Number (A New Question Added Today)

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

  7. LeetCode——Ugly Number

    Description: Write a program to check whether a given number is an ugly number. Ugly numbers are pos ...

  8. LeetCode() Ugly Number II 背下来!

    一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...

  9. LeetCode Ugly Number (简单题)

    题意: 判断是一个数的质因子仅含有2,3,5这3个. 思路: 因子2比较容易解决,num/=num-(num&num-1)就可以了.3和5的需要通过循环来另判. C++ class Solut ...

随机推荐

  1. BZOJ 3736: [Pa2013]Karty

    Description 一个0/1矩阵,求能覆盖所有 \(1\) ,同时不覆盖所有 \(0\) 的矩阵,使这个面积最大. Sol DP/悬线法. 首先,所求的矩阵一定可以覆盖所有贴边的悬线. 用悬线法 ...

  2. hustoj1353 节点选择 树形dp

    1353: 结点选择 时间限制: 1 Sec  内存限制: 128 MB提交: 6  解决: 2[提交][状态][讨论版] 题目描述 问题描述 有一棵 n 个节点的树,树上每个节点都有一个正整数权值. ...

  3. NFS和mount常用参数详解

    NFS权限参数配置 ro 只读访问 rw 读写访问 sync 所有数据在请求时写入共享 async NFS在写入数据前可以相应请求 secure NFS通过1024以下的安全TCP/IP端口发送 in ...

  4. 分治法避免定义多个递归函数,应该使用ResultType

    总结:对二叉树应用分治法时,应避免定义多个递归函数,当出现需要递归求解多种的结果时,尽量使用ResultType来让一次递归返回多种结果. 题目:Binary Tree Maximum Path Su ...

  5. ubuntu add application to launcher

    eg. add sublime text to launcher so as to be found by launcher, docky, etc. add a file sudo gedit /u ...

  6. Ubuntu 15.10下droidbox安装使用

    DroidBox是一个动态分析Android代码的的分析工具.其目前的安装环境为:Linux/Unix/MacOSX 下面是安装步骤 一. 安装Android SDK 并添加环境变量 export P ...

  7. sublime text 2 配置php调试环境

    准备工作: 计算机中已经正确安装了php. 1.设置windows php环境变量->用户变量 Path变量值

  8. Linux中增加软路由的两种方法/删除的方法

    第一种:   route add -net 172.16.6.0 netmask 255.255.255.0 gw 172.16.2.254 dev eth0   route del gw 172.1 ...

  9. Maven学习总结

    转载至:http://www.cnblogs.com/xdp-gacl/p/3498271.html 一 入门 一.Maven的基本概念 Maven(翻译为"专家","内 ...

  10. spinlock原理

    [参考] http://www.searchtb.com/2011/06/spinlock%E5%89%96%E6%9E%90%E4%B8%8E%E6%94%B9%E8%BF%9B.html