[leetcode 263 264]Ugly Number I II
2015-08-31 11:19
387 查看
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
For example,
not ugly since it includes another prime factor
Note that
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test
cases.
AC代码:
Write a program to find the
Ugly numbers are positive numbers whose prime factors only include
For example,
numbers.
Note that
当然是用I中的判断函数一个数字一个数字的判断也是可以的,但是时间效率就不高了
AC代码:
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5.
For example,
6, 8are ugly while
14is
not ugly since it includes another prime factor
7.
Note that
1is typically treated as an ugly number.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test
cases.
AC代码:
class Solution { public: bool isUgly(int num) { if(num==0) return false; while(num!=1) { if(num%2==0) num=num/2; else if(num%3==0) num=num/3; else if(num%5==0) num=num/5; else break; } 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, 12is the sequence of the first
10ugly
numbers.
Note that
1is typically treated as an ugly number.
当然是用I中的判断函数一个数字一个数字的判断也是可以的,但是时间效率就不高了
AC代码:
class Solution { public: int minNum(int x,int y) { return x<y?x:y; } int nthUglyNumber(int n) { if(n==0) return 0; int *count=new int ; count[0]=1; int two=0; int three=0; int five=0; int sum=1; int min=0; while(sum<n) { min=minNum(minNum(count[two]*2,count[three]*3),count[five]*5); if(min>count[sum-1]) { count[sum]=min; ++sum; } if(min==count[two]*2) ++two; else if(min==count[three]*3) ++three; else ++five; } return count[sum-1]; } };
其他Leetcode题目AC代码:https://github.com/PoughER/leetcode
相关文章推荐
- 邀您一起见证我的2014.9--至今一年的成长与收获
- 二年为限,看到自己的改变
- Flowchart
- strncpy函数
- 利用Media Queries来实现IE hack
- .zip.001 .zip.002怎么用压缩包解压缩
- 如何用图表控件实现点击图例图标隐藏图表序列
- Java基础学习第一章练习T1
- 尽情享受每一个字节
- 【zZ】dagger2使用心得(一)
- Javascript鼠标滚轮编程
- Leetcode: House Robber II
- Android同一个xml布局文件在不同中显示效果不同
- IOS 学习 之 引用计数
- IntelliJ IDEA系列教程五 常用快捷键
- 结巴分词学习笔记
- jquery选择器总结
- The Network Adapter could not establish the connection问题研究
- qemu-kvm磁盘读写的缓冲(cache)的五种模式
- 推荐几本 Javascript 书籍