• Ugly Number,Ugly Number II,Super Ugly Number


    一.Ugly Number

    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.

    class Solution {
    public:
        bool isUgly(int num) {
            if(num<=0)return false;
            while(num%2==0){
                num /= 2;
            }
            while(num%3==0){
                num /= 3;
            }
            while(num%5==0){
                num /= 5;
            }
            return num==1 ? true:false;
        }
    };

    二.Ugly Number II

    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.

    class Solution {
    public:
        int nthUglyNumber(int n) {
            int index2=0,index3=0,index5=0;
            int ugly2=0,ugly3=0,ugly5=0;
            vector<int> res(n,1);
            int index = 1;
            while(index < n){
                while(res[index2]*2 <= res[index-1]){
                    index2++;
                }
                while(res[index3]*3 <= res[index-1]){
                    index3++;
                }
                while(res[index5]*5 <= res[index-1]){
                    index5++;
                }
                ugly2 = res[index2]*2;
                ugly3 = res[index3]*3;
                ugly5 = res[index5]*5;
                res[index++] = min(ugly2,min(ugly3,ugly5));
            }
            return res[n-1];
        }
    };

    Super Ugly Number

    Total Accepted: 2860 Total Submissions: 9791 Difficulty: Medium

    Write a program to find the nth super ugly number.

    Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

    Note:
    (1) 1 is a super ugly number for any given primes.
    (2) The given numbers in primes are in ascending order.
    (3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.

    class Solution {
    public:
        int nthSuperUglyNumber(int n, vector<int>& primes) {
            int primes_size = primes.size();
            vector<int> vec_indexs(primes_size,0) ,vec_uglys(n,1)   ;
            for(int i=1;i<n;++i){
                int min_ugly = INT_MAX;
                for(int j=0;j<primes_size;++j){
                    int index = vec_indexs[j];
                    while(primes[j]*vec_uglys[index] <= vec_uglys[i-1]){
                        index++;
                    }
                    min_ugly      = min(min_ugly,primes[j]*vec_uglys[index]);
                    vec_indexs[j] = index;
                }
                vec_uglys[i] = min_ugly;
            }
            return vec_uglys.back();
        }
    };
  • 相关阅读:
    腾讯云发布“创新成长快线”,首期向创业者赠送10亿分钟实时音视频时长
    Tencent Kona JDK11正式开源,腾讯大数据将持续贡献Java生态发展
    腾讯视频云勇夺云端视频转码大赛多项第一
    分享一些常用的开源博客社区网站
    分享些发表技术类文章的平台
    Linux之ps命令基本使用
    彻底卸载 Oracle11g r2 教程(亲测有效,已重装过)
    Oracle11g R2 安装教程(非常详细 )
    苹果CMS搭建影视网站教程
    JSON 基本使用
  • 原文地址:https://www.cnblogs.com/zengzy/p/5002255.html
Copyright © 2020-2023  润新知