Ugly Number II -- leetcode

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.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
  4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).

算法一,DP逐个生成ugly numbers

1. ugly number初始为1

2. 每次循环生成一个新的次大ugly number

即从小到大生成ugly number。

3. 从已经生成的ugly number中,挑选一个,使其*2, 或者*3, *5,得到下一个ugly number。

即 = min(x*2, min(y*3, z*5))  x, y, z为已经生成的ugly number

class Solution {
public:
    int nthUglyNumber(int n) {
        vector<int> uglys(n, 1);
        int id2 = 0, id3 = 0, id5 = 0;
        for (int i=1; i<n; i++) {
            uglys[i] = min(uglys[id2]*2, min(uglys[id3]*3, uglys[id5]*5));
            if (uglys[i] == uglys[id2]*2)
                ++id2;
            if (uglys[i] == uglys[id3]*3)
                ++id3;
            if (uglys[i] == uglys[id5]*5)
                ++id5;
        }
        return uglys[n-1];
    }
};


算法一开始分配了n个存储空间,用来存储n个ugly number。

但在生成新的ugly number时,前面的逐渐开始不需要了。

将vector,改作list,这样,可以把前面不再需要的ugly number逐渐删除掉,以节省空间。


class Solution {
public:
    int nthUglyNumber(int n) {
        list<int> uglys;
        uglys.push_back(1);
        auto iter2 = uglys.begin();
        auto iter3 = uglys.begin();
        auto iter5 = uglys.begin();
        for (int i=1; i<n; i++) {
            uglys.push_back(min(*iter2 * 2, min(*iter3 * 3, *iter5 * 5)));
            if (*iter2 * 2 == uglys.back())
                ++iter2;
            if (*iter3 * 3 == uglys.back())
                ++iter3;
            if (*iter5 * 5 == uglys.back())
                ++iter5;
            while (*uglys.begin() < *iter5)
                uglys.pop_front();
        }
        return uglys.back();
    }
};


算法二,显示归并

和算法一思路差不多。算法一,隐式有三个队列,并作归并。

此处,显示的设置三个队列。

1. 每当求得一个新的ugly number,将期*2, *3, *5,并分别送入三个队列。

2. 对这个三队列作归并,求出一个新的ugly number。

3. 重复步骤1,2

class Solution {
public:
    int nthUglyNumber(int n) {
        int ans = 1;
        --n;
        queue<int> L2, L3, L5;
        while (n--) {
            L2.push(ans * 2);
            L3.push(ans * 3);
            L5.push(ans * 5);
            while (L2.front() <= ans) L2.pop();
            while (L3.front() <= ans) L3.pop();
            while (L5.front() <= ans) L5.pop();
            ans = min(L2.front(), min(L3.front(), L5.front()));
        }
        return ans;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值