leetcode Ugly Number II

题目连接

https://leetcode.com/problems/ugly-number-ii/

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.

class Solution {
private:
    typedef unsigned long long int ull;
    struct cmp {
        bool operator()(const ull a, const ull b) {
            return a > b;
        }
    };
public:
    int nthUglyNumber(int n) {
        ull lb = 0, ub = 0, arr[3] = { 2, 3, 5 };
        priority_queue<ull, vector<ull>, cmp > q;
        q.push(1);
        for (int i = 1; i <= n;) {
            ull val = q.top(); q.pop();
            ub = val;
            if (ub == lb) {
                continue;
            } else {
                lb = val;
                i++;
            }
            for (int j = 0; j < 3; j++) q.push(val * arr[j]);
        }
        return (int)lb;
    }
};
时间: 2024-10-13 16:58:15

leetcode Ugly Number II的相关文章

[LeetCode] 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

[LeetCode] Ugly Number II (A New Question Added Today)

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

LeetCode() Ugly Number II 背下来!

一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.size() < n) { results.push_back(min(results[i] * 2, min(results[j] * 3, results[k] * 5))); if (results.back() == results[i] * 2) ++i; if (results.back()

LeetCode(264):Ugly Number II

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

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

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.

【Leetcode】Ugly Number II

题目链接:https://leetcode.com/problems/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

leetcode笔记: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 treat

[LeetCode]70. Ugly Number II第N个丑数

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