zoukankan      html  css  js  c++  java
  • 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();
        }
    };
  • 相关阅读:
    用before 和after 清除浮动
    清除浮动最优
    pc端布局03
    PC端布局02
    >PC端常用布局01
    浮动
    盒模型-
    盒模型-外边距合并
    spring中的AOP
    AOP的概念
  • 原文地址:https://www.cnblogs.com/zengzy/p/5002255.html
Copyright © 2011-2022 走看看