题目:

把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

思路:

1、逐个判断

逐个判断每个整数是不是丑数。根据丑数的定义,丑数只能被2,3,5整除,也就是说,如果一个数能被2整除,连续除以2,如果能被3整除,连续除以3,如果能被5整除,连续除以5,如果最后得到1,那么这个数就是丑数,否则就不是。

2、创建数组保存已经找到的丑数

第一种方法效率比较低,因为该方法对每个数无论是丑事还是非丑数,都进行了计算判断,这在一定程度上是浪费,有没有可能避免这种浪费呢?

根据丑数的定义,后面的丑数应该是前面丑数的2,3,5的倍数结果,因此我们可以创建一个数组,里面的数字是排好序的丑数,每一个丑数都是前面的丑数乘以2,3,5得到。

那么如何保证排序呢?假设当前数组中最大的丑数为M,我们把已有的每个丑数分别乘以2,3,5(实际上并不需要对每个丑数做乘积,只需每次保存上一次的位置即可),分别得到第一个乘以2后大于M的结果M2,第一个乘以3后大于M的结果M3,第一个乘以5后大于M的结果M5,那么下一个丑数就是M2,M3,M5的最小者。

代码:

1、逐个判断

bool isUgly(int n){
while(n%2==0)
n=n/2;
while(n%3==0)
n=n/3;
while(n%4==0)
n=n/5;
if(n==1)
return true;
return false;
} int getUglyNumber(int index){
if(index<=0)
return 0;
int number=1;
int count=0;
while(count<=index){
if(isUgly(number))
count++;
number++;
}
return number;
} class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index<=0)
return 0;
int count=0;
int number=0;
while(count<index){
++number;
if(isUgly(number))
count++;
}
return number;
} bool isUgly(int n){
while(n%2==0)
n=n/2;
while(n%3==0)
n=n/3;
while(n/5==0)
n=n/5;
return (n==1)?true:false;
}
};

2、只对丑数做操作

int min_3(int num1,int num2,int num3){
num1=(num1<num2)?num1:num2;
num1=(num1<num3)?num1:num3;
return num1;
} int getUglyNumber_1(int index){
if(index<=0)
return 0;
int *uglyNumbers=new int[index];
uglyNumbers[0]=1;
int nextIndex=1; int *index_2=uglyNumbers;
int *index_3=uglyNumbers;
int *index_5=uglyNumbers; while(nextIndex<index){
uglyNumbers[nextIndex]=min_3(*index_2*2,*index_3*3,*index_5*5);
while(*index_2*2<=uglyNumbers[nextIndex]) ++index_2;
while(*index_3*3<=uglyNumbers[nextIndex]) ++index_3;
while(*index_5*5<=uglyNumbers[nextIndex]) ++index_5;
++nextIndex;
} int ugly=uglyNumbers[index-1];
delete[] uglyNumbers;
return ugly;
}

在线测试OJ:

http://www.nowcoder.com/books/coding-interviews/6aa9e04fc3794f68acf8778237ba065b?rp=2

AC代码:

class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index<=0)
return 0;
int uglyNumbers[index];
uglyNumbers[0]=1;
int nextIndex=1;
int index_2=0;
int index_3=0;
int index_5=0; while(nextIndex<index){
uglyNumbers[nextIndex]=min_3(uglyNumbers[index_2]*2,uglyNumbers[index_3]*3,uglyNumbers[index_5]*5);
while(uglyNumbers[index_2]*2<=uglyNumbers[nextIndex]) ++index_2;
while(uglyNumbers[index_3]*3<=uglyNumbers[nextIndex]) ++index_3;
while(uglyNumbers[index_5]*5<=uglyNumbers[nextIndex]) ++index_5;
++nextIndex;
}
return uglyNumbers[index-1];
} int min_3(int num1,int num2,int num3){
num1=(num1<num2)?num1:num2;
num1=(num1<num3)?num1:num3;
return num1;
} };

  

(剑指Offer)面试题34:丑数的更多相关文章

  1. 剑指Offer:面试题34——丑数(java实现)

    问题描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 思路1: ...

  2. 剑指Offer - 九度1214 - 丑数

    剑指Offer - 九度1214 - 丑数2013-11-21 21:06 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. ...

  3. 【剑指offer 面试题34】丑数

    只包含因子2.3.5的数称作丑数. #include <iostream> #include <vector> using namespace std; int GetUgly ...

  4. 剑指offer系列59---寻找丑数

    [题目]把只包含因子2.3和5的数称作丑数(Ugly Number). * 例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解法一 ...

  5. 【剑指Offer】33、丑数

      题目描述:   把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数 ...

  6. 【剑指offer】q34:丑数

    题目要求第n个丑数.所以对于中间结果不须要保存. def Humble(index): curHum = 1 M2 = 2; M3 = 3; M5 = 5 while index > 1: cu ...

  7. 剑指offer(33)丑数

    题目描述 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题目分析 ...

  8. 剑指offer三十三之丑数

    一.题目 如果一个数的因子中,出去1和本身以外,质数因子只包含2.3和5,则把改数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质数因子7. 习惯上我们把1当做是第一个 ...

  9. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  10. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

随机推荐

  1. oracle数据库重建EM

    首先直接在文本控制台执行: [emca不像dbca.netca一样会出现图形化的界面,而是通过文本的交互式操作来完成重新配置]   emca -config dbcontrol db -repos   ...

  2. webpack的学习

    什么是webpack? 他有什么优点? 首先对于很多刚接触webpack人来说,肯定会问webpack是什么?它有什么优点?我们为什么要使用它?带着这些问题,我们来总结下如下: Webpack是前端一 ...

  3. JBPM4入门——7.等待节点的单条线手动执行

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...

  4. 经典排序算法(Java版)

    1.冒泡排序 Bubble Sort 最简单的排序方法是冒泡排序方法.这种方法的基本思想是,将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮.在冒泡排序算法中我们要对这个“气泡” ...

  5. head 命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  6. UIImageView 点击放大缩小

    static CGRect oldframe; -(void)showImage:(UIImageView *)avatarImageView{ UIImage *image=avatarImageV ...

  7. HW7.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. MapSearch 阅读随笔

    MapSearch https://developer.apple.com/library/ios/samplecode/MapSearch/Introduction/Intro.html#//app ...

  9. dom div移动解决停顿问题

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 开源的c语言人工神经网络计算库 FANN

    这年头机器学习非常的火,神经网络算是机器学习算法中的比较重要的一种.这段时间我也花了些功夫,学了点皮毛,顺便做点学习笔记. 介绍人工神经网络的基本理论的教科书很多.我正在看的是蒋宗礼教授写的<人 ...