263/264. Ugly Number / Ugly Number II

263. Ugly Number

My Submissions
Question
Total Accepted: 33755  Total Submissions: 96873  Difficulty: Easy

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags
  Math
Show Similar Problems

分析:

题目说的很清楚,所谓丑数,就是那些因子只含2,3,5的数
那么根据丑陋数的定义,我们将给定数除以2、3、5,直到无法整除,也就是除以2、3、5的余数不再为0时停止
这时如果得到1,说明是所有因子都是2或3或5,如果不是1,则不是丑陋数。

class Solution {
public:
    bool isUgly(int num) {
       if(num<=0) 
             return false;  
       if(num==1) 
             return true;  
          
        while(num>=2 && num%2==0) //先将因子2除完,接着3,5
            num/=2;  
        while(num>=3 && num%3==0) 
            num/=3;  
        while(num>=5 && num%5==0) 
            num/=5;  
          
        return num==1;  
    }
};



264. Ugly Number II

My Submissions
Question
Total Accepted: 18482  Total Submissions: 74291  Difficulty: Medium

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number.

Hint:

  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.Show More Hint 

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags
  Dynamic Programming Heap Math
Show Similar Problems









//首先思路:提示说的很清楚,第n个丑数的沿途大量的数都不是丑数,为了能跳过对他们的判断
//直接由小到大生成丑数进行统计个数,直到是第n个为止
//用三个临时数和数组来渐进增大丑数
class Solution {
public:
    int nthUglyNumber(int n) 
    {
        vector<int> ugly(n,0);//记录沿途的丑数
        ugly[0]=1;  
        int figure2=2,figure3=3,figure5=5;  //渐进增长的丑数变量
        int index2=0,index3=0,index5=0;  //丑数变量增长时是渐进地来自前面较小的丑数
        for(int i=1;i<n;++i)  
        {  
            ugly[i]=min(figure2,min(figure3,figure5));//根据数列得到最小的值  
            if(figure2==ugly[i])
                figure2=2*ugly[++index2];  
            if(figure3==ugly[i]) 
                figure3=3*ugly[++index3];  
            if(figure5==ugly[i]) 
                figure5=5*ugly[++index5];  
              
        }  
        return ugly[n-1];
    }
};


九度题源:

题目描述:

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

输入:

输入包括一个整数N(1<=N<=1500)。

输出:

可能有多组测试数据,对于每组数据,
输出第N个丑数。

样例输入:
3
样例输出:
3

#include "vector"
#include "string"
#include "algorithm"
#include <iostream>
 
using namespace std;
 
void countUglyNums(vector<int> &vec)
{
    int t2 = 1, t3 = 1, t5 = 1;
    int cnt = 1;
    while (cnt <= 1501)
    {   //用2,3,5这三个数相乘来组成丑数
        //唯一需要确保的就是我们组成的丑数是按从小到大的关系出现的
        //用三个数来维护渐乘的结果,总是选出最小的丑数
        int a = vec[t2] * 2, b = vec[t3] * 3, c = vec[t5] * 5;
        int minUgly = min(min(a, b), c);//求三数中最小的一个
        vec.push_back(minUgly);//第cnt个丑数总是在数组末尾
        //更新丑数,新丑数必须必原丑数大
        while (a <= minUgly)
        { 
            ++t2; 
            a = vec[t2] * 2; 
        }
        while (b <= minUgly)
        { 
            ++t3;
            b = vec[t3] * 3;
        }
        while (c <= minUgly)
        { 
            ++t5; 
            c = vec[t5] * 5; 
        }
        cnt++;
    }
}
 
int main()
{
    int n;
    vector<int> vec;
    vec.push_back(0);
    vec.push_back(1);
    countUglyNums(vec);
    while (cin>>n)
    {
        cout << vec[n] << endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1214
    User: EbowTang
    Language: C++
    Result: Accepted
    Time:20 ms
    Memory:1520 kb
****************************************************************/




参考资源:

【1】网友,tyq101010,原文地址, http://blog.csdn.net/tyq101010/article/details/49256889


你可能感兴趣的:(LeetCode,数据结构,算法,面试,ACM)