C++ solution with priority-queue


  • 2
    L
    int nthUglyNumber(int n) {
        unordered_set<long long> Numset;
        priority_queue<long long, vector<long long>, greater<long long>> ugpq;
        ugpq.push(1);
        int arr[] = {2, 3, 5};
        int i = 0;
        long long num = 1;
        
        while(i < n)
        {
            num = ugpq.top();
            ugpq.pop();
            if(++i == n)
            {
                break;
            }
            else
            for(int j = 0; j < 3; j++)
            {
                if(!Numset.count(arr[j]*num))
                {
                    Numset.insert(arr[j]*num);
                    ugpq.push(arr[j]*num);
                }
            }
        }
        return num;
    }

  • 0
    B

    I use arr[j] * num > 0 to judge if it is overflow instead of long long. However, it comes out to be wrong answer. Could you help me out?


  • 0
    L

    well, I wonder how arr[j] * num > 0 judge it it overflow.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.