leetcode 263|264|313. Ugly Number 1|2 313. Super Ugly Number

来源:互联网 发布:excel数据抽样 编辑:程序博客网 时间:2024/06/07 07:11

263. 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 = num/2;        }        while (num % 3 == 0)        {            num = num/3;        }        while (num % 5 == 0)        {            num = num/5;        }        return num == 1;    }};


264. 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, 12is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number, and n does not exceed 1690.

利用set自动排序的功能。


class Solution {public:    int nthUglyNumber(int n)     {        int count = 0;        set<long long> se;        se.insert(1);        while (1)        {            long long key = *se.begin();                se.erase(key);            if ( ++count == n)                return key;            se.insert(key * 2);                se.insert(key * 3);              se.insert(key * 5);          }        return -1;    }};

还有另外一种方法,val2 val3 val5里面中最小的那个就是下一个填进去的值,填进去之后,index后移,index2 就是 下一个*2的候选值的下标 

class Solution {public:    int nthUglyNumber(int n)     {        vector<int> vc(n, 1);        int index2 = 0;        int index3 = 0;        int index5 = 0;        int val2 = 2;        int val3 = 3;        int val5 = 5;                for (int i = 1; i < n; i++)        {            int insert = min(val2, min(val3, val5));            vc[i] = insert;            if (insert == val2)            {                val2 = vc[++index2] * 2;            }            if (insert == val3)            {                val3 = vc[++index3] * 3;            }            if (insert == val5)            {                val5 = vc[++index5] * 5;            }        }        return vc[n - 1];    }};

313. Super Ugly Number

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.
(4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer.

方法和上面那个一模一样。

class Solution {public:    int nthSuperUglyNumber(int n, vector<int>& primes)     {           vector<int> index(primes.size(), 0); //index[i] 存的是primes第i位应该和 index[i]相乘        vector<int> ret(n, 1);  //存结果数组        vector<int> val(primes);        for (int i = 1; i < n; i++)        {            ret[i] = *min_element(val.begin(), val.end());            for (int j = 0; j < val.size(); j++)            {                if (ret[i] == val[j])                {                    val[j] = ret[++index[j]] * primes[j];                }            }        }        return ret[n - 1];    }};




原创粉丝点击