丑数问题 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个丑数. 解法一 ...
随机推荐
- VueJS 数据驱动和依赖追踪分析
之前关于 Vue 数据绑定原理的一点分析,最近需要回顾,就顺便发到随笔上了 在之前实现一个自己的Mvvm中,用 setter 来观测model,将界面上所有的 viewModel 绑定到 model ...
- 使用 JsPlumb 绘制拓扑图的通用方法
摘要: 实现 JsPlumb 绘制拓扑图的通用方法. 只要服务端返回一个符合指定格式的数据结构,就可以绘制相应的拓扑图. 难度: 中级 示例工程见: http://download.csdn.net ...
- python-自定义异常,with用法
抛出异常 #coding=utf-8 def exceptionTest(num): if num<0: print "if num<0" raise Excepti ...
- linux服务器---安装swat
安装swat swat是一个图形化的samba管理软件,可以帮助不熟悉的人去灵活的配置samba服务, 1.安装swat [root@localhost wj]#yum install -y samb ...
- iOS可执行文件__TEXT段限制 以及 Android 65K函数限制
1.先看下苹果关于 .ipa上传的大小规定: 最大构建版本文件大小 解压 XXX.ipa size Payload/xxx.app/xxx 32位 32位 + 64位 有些2dx.u3d游戏 或是 ...
- 算法之路 level 01 problem set
2992.357000 1000 A+B Problem1214.840000 1002 487-32791070.603000 1004 Financial Management880.192000 ...
- 20145333茹翔 Exp5 利用nmap扫描
20145333茹翔 Exp5 利用nmap扫描 实验过程 首先使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsol ...
- PN结讲解
可能大家在使用半导体器件的时候只是在使用它的电气属性,并没有很好的关心下它是什么原因才有了这样的电气属性,那么我们本篇就从物理结构分析下PN结吧. 首先看一张比较陈旧的图图: (就按自己的笔记简单谈谈 ...
- 使用volley来json解析
我对网络请求get和post的理解: 1.get只是从某网址获得固定数据,如我访问百度,返回就是百度的html语句: 2.post是我在访问的时候加了某些参数,如我访问某个服务器,访问的时候加了一些语 ...
- Ruby基础教程
一.Ruby基础知识 1.关于Ruby Ruby是脚本语言 Ruby是面向对象语言 Ruby是跨平台语言 Ruby是开放源码软件 2.Ruby入门书籍推荐 <Ruby.Programming向R ...