丑数问题 Ugly Number
2018-07-28 15:30:21
一、判断是否为丑数
问题描述:

问题求解:
所谓丑数,首先得是正数,然后其质数因子只包含了2,3,4,因此我们只需要对当前的数分别除2,3,4直到不能除为止。
public boolean isUgly(int num) {
if (num > 0) {
for (int i = 2; i < 6; i++) {
while (num % i == 0) num /= i;
}
}
return num == 1;
}
二、第n个丑数
问题描述:

问题求解:
由上面检测丑数的解法我们可以知道,每次丑数的生成都是使用2,3,5来乘已经生成的丑数,取其中最小的。为了降低不必要的比较,我们需要将当前2,3,5需要乘的丑数的下标保存下来,利用下标来进行快速的计算和判断。
public int nthUglyNumber(int n) {
int[] res = new int[n];
int[] idx = new int[3];
res[0] = 1;
for (int i = 1; i < n; i++) {
res[i] = Math.min(2 * res[idx[0]], Math.min(3 * res[idx[1]], 5 * res[idx[2]]));
if (res[i] == 2 * res[idx[0]]) idx[0]++;
if (res[i] == 3 * res[idx[1]]) idx[1]++;
if (res[i] == 5 * res[idx[2]]) idx[2]++;
}
return res[n - 1];
}
三、丑数问题的扩展
问题描述:

问题求解:
本题其实就是将丑数问题中质数从2,3,5扩展到了primes数组,本质的解法是一样的。
public int nthSuperUglyNumber(int n, int[] primes) {
int[] res = new int[n];
int[] idx = new int[primes.length];
res[0] = 1;
for (int i = 1; i < n; i++) {
res[i] = Integer.MAX_VALUE;
for (int j = 0; j < primes.length; j++) {
res[i] = Math.min(res[i], primes[j] * res[idx[j]]);
}
for (int j = 0; j < primes.length; j++) {
if (primes[j] * res[idx[j]] <= res[i]) idx[j]++;
}
}
return res[n - 1];
}
这里可以对内层循环进行优化,将两次循环降低到一次循环,要做到这一步,就需要再申请大小为primes.length的数组,用来保存当前乘积。
public int nthSuperUglyNumber2(int n, int[] primes) {
int[] res = new int[n];
int[] idx = new int[primes.length];
int[] val = new int[primes.length];
Arrays.fill(val, 1);
int next = 1;
for (int i = 0; i < n; i++) {
res[i] = next;
next = Integer.MAX_VALUE;
for (int j = 0; j < primes.length; j++) {
if (val[j] == res[i]) val[j] = primes[j] * res[idx[j]++];
next = Math.min(next, val[j]);
}
}
return res[n - 1];
}
丑数问题 Ugly Number的更多相关文章
- [Swift]LeetCode264.丑数 II | Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [Swift]LeetCode313. 超级丑数 | Super Ugly Number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
- C#LeetCode刷题之#263-丑数(Ugly Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3862 访问. 编写一个程序判断给定的数是否为丑数.丑数就是只包含 ...
- 37.寻找丑数[Ugly numbers]
[题目] 我们把只包含质因子2.3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第1500个丑 ...
- 丑数(Ugly Numbers, UVa 136)
丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...
- 剑指Offer面试题:29.丑数
一.题目:丑数 题目:我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做第一个 ...
- 剑指Offer:面试题34——丑数(java实现)
问题描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 思路1: ...
- 剑指offer系列59---寻找丑数
[题目]把只包含因子2.3和5的数称作丑数(Ugly Number). * 例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解法一 ...
随机推荐
- [转]Visual C++ RunTime的特征——非烫即屯
Visual C++ RunTime的特征——非烫即屯 大一刚学C语言,第二次上机课,当我发现我照着书抄写的程序在运行之外的黑框里面跳出一排“烫烫烫烫烫”,当时就震惊了.你们能想象一个来自小城, 在大 ...
- tfs代码上传到server并下载到新位置
1.svn与git代码管理原理基本一致,基于文档管理,能看到文件代码,通过设置文件的只读属性来控制代码. 而tfs是基于sqlserver及lock来管理,看不见代码文件 2.tfs没有自己的用户管理 ...
- python在交互模式下直接输入对象后回车,调用的是对象的__repr__()方法,这个方法表示的是一个编码,用print+对象是调用对象的__str__方法
交互模式下调用对象的__repr__()方法,这个方法表示的是一个编码 >>> u"国庆节快乐"u'\u56fd\u5e86\u8282\u5feb\u4e50' ...
- 0-5v转0-20ma和0-5v转4-20ma
0-5v转0-20ma 0-5v转4-20ma
- P3313 [SDOI2014]旅行
P3313 [SDOI2014]旅行 树链剖分+动态线段树(并不是lct) 显然的,我们对于每一个宗教都要维护一个线段树. (那么空间不是爆炸了吗) 在这里引入:动态开点线段树 就是需要的点开起来,不 ...
- centos下nginx安装与配置
nginx依赖以下模块: l gzip模块需要 zlib 库 l rewrite模块需要 pcre 库 l ssl 功能需要openssl库 tar xzvf nginx-1.9.15.tar. ...
- 02: shell中的if、case、for等语句
目录: 1.1 shell中常用运算符 1.2 使用if条件语句 1.3 shell 中的for循环 1.4 shell中的while循环语句 1.5 使用case分支语句 1.1 shell中常用运 ...
- Win32 API编程:使用CreateProcess创建新进程
#include <windows.h> #include <tchar.h> #include <stdio.h> int main(int argc, char ...
- Linux多线程--使用互斥量同步线程【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10875883 前文再续,书接上一回,在上一篇文章:Linux多线程——使用信号量同步线程中, ...
- P4113 [HEOI2012]采花 (莫队TLE)
思路 update 11.2 树状数组AC 本题莫队过不去,会TLE ----------------------- 但也是个不错的莫队练手题 ------------------------ 毕竟C ...