剑指offer34——丑数
来源:互联网 发布:扫描仪软件 绿色版 编辑:程序博客网 时间:2024/06/16 03:32
- 题目描述
- 简单粗暴的法一
- 某个丑数必是前面某个丑数乘因子得到的
题目描述
我们把只包含因子2、3、5的数称为丑数。求从小到大顺序第1500个丑数。习惯上把1称为第一个丑数。
简单粗暴的法一
直接判断每个数是不是丑数,直到找到第1500个为止
bool isUgly(int num){ while(num%2==0) num=num/2; while(num%3==0) num=num/3; while(num%5==0) num=num/5; if(num==1) return true; else return false;}
某个丑数必是前面某个丑数乘因子得到的
class Solution {public: int GetUglyNumber_Solution(int index) { if(index<7) return index; vector<int> res(index); res[0]=1; int t2=0; int t3=0; int t5=0; for(int i=1;i<index;i++){ res[i]=min(res[t2]*2,min(res[t3]*3,res[t5]*5)); if(res[i]==res[t2]*2) t2++; if(res[i]==res[t3]*3) t3++; if(res[i]==res[t5]*5) t5++; } return res[index-1]; }};
用t2,t3,t5分别记录目前的最大数是由那个得到的,比如如果目前已知的最大丑数是由res[i*2]
得到的,则t2=i。但要注意的是,必须2,3,5都要判断一遍,而不是用if,else语句。
0 0
- 剑指offer34——丑数
- 剑指Offer34 丑数
- 剑指offer34:丑数
- offer34--丑数
- 剑指offer—丑数
- 剑指offer—丑数
- 剑指offer ——寻找丑数
- 《剑指offer》——丑数
- 剑指offer——丑数
- 剑指Offer——丑数
- 剑指offer——丑数
- 剑指Offer——丑数
- 剑指offer——丑数
- 剑指offer——丑数
- 剑指Offer—33—丑数
- 剑指offer——33.丑数
- POJ1338——丑数
- 华为技术——丑数
- 1056. Mice and Rice (25)PAT甲级
- node.js中express的安装及调试流程的整合
- Java南阳OJ-ASCII码排序
- Mysql profile分析查询
- 模板类中友元函数的声明与定义
- 剑指offer34——丑数
- 3.6 最大公约数
- 寒假16:算法训练 Hankson的趣味题
- 算24点小游戏
- Mysql Union 联合查询的使用
- VTK修炼之道50:图形基本操作进阶_网格模型的特征边 与 封闭性检测
- python学习笔记2
- Mysql统计所有表数据量
- 大数相加 大数相乘 C++