Ugly Number II

public class Solution {
    /**
     * @param n: An integer
     * @return: return a  integer as description.
     */
    public int nthUglyNumber(int n) {
        // write your code here
        PriorityQueue<Long> minHeap;
        minHeap = new PriorityQueue<Long>();
        minHeap.offer(new Long(1));
        int count = 0;
        long lst = -1;
        while(count < n){
            count ++;
            while(minHeap.peek() == lst) minHeap.poll();
            Long now = minHeap.poll();
            minHeap.offer(now * 2);
            minHeap.offer(now * 3);
            minHeap.offer(now * 5);
            lst = now;
        }
        return (int)lst;
    }
}

最后更新于

这有帮助吗?