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. [mysql] Some non-transactional changed tables couldn't be rolled back

    使用peewee的事务时,碰到一个郁闷的问题,事务似乎无效! 于是简化了下模型,写了简单的测试代码,发现问题,如题所示. 找到解答: https://github.com/etianen/django ...

  2. HTTP Servlet 重要的几个方法

    HTTP Servlet继承了GencenServlet类    GencenServlet实现了两个接口··一个用于ServletConfig设置接口,一个为Servlet接口只要是(1) init ...

  3. Buzz words

    给你一个字符串和字典,从头扫到位,如果到当前的字符的字符串存在于字典中,则显示 buzz. 例子: ILOVEPINEAPPLEJUICE 字典: [pine, apple, pineapple, j ...

  4. BIOS开启虚拟化

    启动时根据提示按del 键按 F10 键以配置 BIOS使用箭头键滚动到“System Configuration”选择“Virtualization Technology”,然后按 Enter 键选 ...

  5. Java GUI学习笔记之初识AWT和Swing

    Frame f = new Frame(); //获取显示器的尺寸 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize() ...

  6. OGNL表达式

    一.什么是OGNL,有什么特点?     OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言 ...

  7. 【XLL 文档翻译】【第3部分】必要的和有用的 C API XLM 函数

    本节中将介绍几个对于 DLL 和 XLL 开发人员来说十分重要的回调函数,xlfRegister 函数是可用于注册函数,使得 Excel 可以直接访问 DLL 和 XLl 中的函数. xlfUnreg ...

  8. linux日常易忘指令

    1.编辑全局指令 进入~/.bash_profile 增加"exprot +(启动文件的地址)" source ~/.bash_profilr(刷新) 2.修改mysql密码 my ...

  9. iOS中UITableView的一些设置

    不可滑动: ? 1 tableView.userInteractionEnabled = NO; 也可以在storyboard中的userInteractionEnable属性设置 显示导向箭头: ? ...

  10. iOS高效调试

    写代码难免出现bug. 储备些调试技能绝对能够提高你的工作效率,让bug无所遁形.下面就和大家分享一些我在工作中常用的iOS调试小技能. 1. 打印 最简单,基础的调试方法就是打印日志了.贴出两段封装 ...