丑数
来源:互联网 发布:js下载图片保存到本地 编辑:程序博客网 时间:2024/05/09 05:43
把只包含素因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
本来打算暴力跑出所有的丑数,结果发现更巧妙地可以只储存丑数,利用丑数求丑数,嗯
class Solution {public: int GetUglyNumber_Solution(int index) { int a[100005]; a[0] = 1; int cnt = 0; int w2 = 0, w3 = 0, w5 = 0; while (cnt <= index) { int mmin = min(a[w2]*2, min(a[w3]*3, a[w5]*5)); a[++cnt] = mmin; while (a[w2] * 2 <= a[cnt]) w2++; while (a[w3] * 3 <= a[cnt]) w3++; while (a[w5] * 5 <= a[cnt]) w5++; } return a[index-1]; }};
0 0
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- 丑数
- html+css面试题
- servlet解析客户端http请求流程
- MongoDb .net Official Driver 一些用法
- activiti学习笔记7--Deployment cache configuration--20170412
- Java8 新特性之集合: removeIf(Predicate<? super E> filter)
- 丑数
- 利用python对网页和图片的简单爬取
- Spring 下载教程 (适用于最新版Spring官网)
- 什么是时间复杂度,什么是P问题、NP问题和NPC问题
- linux命令详解之cd命令
- 单例设计模式
- 第十二单元练习
- 51单片机按键检测和信号函数仿真
- 冒泡算法和选择排序