Leetcode 263 & 264 & 313

Leetcode 263 & 264 & 313

Leetcode 263 Ugly Number

Description

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.

题解

题意很简单,只含2,3,5作为因子的数就是一个ugly number,判断一个数字是否为ugly number就判断能不能求余然后不断相除,最后判断结果是否为1。

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;
    }
};

Leetcode 264 Ugly Number II

Description

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, and n does not exceed 1690.

题解

题目跟上一题基本无关,同样是关于ugly number,但是是求出第n大的ugly number,一开始做的时候是懵逼的,后来想一下可以用动态规划的办法,用3个int t2、t3、t5来分别记录以2、3、5作为因子的index,然后以vector k中的数字作为另一个因子,每一次乘一次对应的index+1。注意的是中间要判断取所有乘积中的最小值。

class Solution {
public:
    int nthUglyNumber(int n) {
        if (n <= 0)
            return -1;
        if (n == 1)
            return 1;
        vector<int> k;
        int t2 = 0, t3 = 0, t5 = 0;
        k.push_back(1);
        // 动态规划保存前面的数字
        for (int i = 1; i < n; i++) {
            k.push_back(min(k[t2] * 2, min(k[t3] * 3, k[t5] * 5)));
            if (k[i] == k[t2] * 2) t2++;
            if (k[i] == k[t3] * 3) t3++;
            if (k[i] == k[t5] * 5) t5++;
        }
        return k[n - 1];
    }
};

Leetcode 313 Super Ugly Number

Description

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.

题解

和264相似,也是用动态规划的办法不断记录上一次相乘的积用作下一次的计算,因为作为ugly number的因子是不确定的所以用一个vector来储存因子,并且因为要排除有不同组合的因子相乘得到的结果相同的情况,所以需要有额外的一步判断,判断乘出来的数字是否和总的vector的最后一个数字相同,相同则排除。

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        int size = primes.size();
        vector<int> vec;
        vec.push_back(1);
        vector<int> primesIndex(size, 0);
        while (vec.size() < n) {
            int tempMin = INT_MAX, tempIndex;
            for (int j = 0; j < size; j++) {
                if (tempMin > vec[primesIndex[j]] * primes[j]) {
                    tempMin = vec[primesIndex[j]] * primes[j];
                    tempIndex = j;
                }
            }
            if (vec[vec.size() - 1] < tempMin) {
                vec.push_back(tempMin);
            }
            primesIndex[tempIndex]++;
        }
        return vec[n - 1];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值