• 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 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).

    解题思路:

    方法一: Priority Queue, 取出最小值乘以2,3,5, 加入到Heap中,然后再取出最小值,循环n-1次,最后得到的最小值就是结果。因为用到了现成的Priority Queue, 花时间较多。

    方法二: declared three counter variables: a,b,and c which represent the corresponding index in the arraylist for the multiplier of 2,3,and 5. Since each previous ugly number times one of the multiplier will produce a new ugly number, I start from the starting index 0 and multiply the ugly number at that index with each multiplier and get the smallest product which is the next ugly number from the three. The corresponding multipliers' index will be incremented by one and we do this recursively until we have K ugly numbers. 


    Java code:

    方法一:

    public class Solution {
        public int nthUglyNumber(int n) {
            if(n == 1){
                return 1;
            }
            PriorityQueue<Long> q = new PriorityQueue<Long>();
            q.add(1l);
            
            for(long i = 1; i < n; i++) {
                long tmp = q.poll();
                while(!q.isEmpty() && q.peek() == tmp) {
                    tmp = q.poll();
                }
                q.add(tmp*2);
                q.add(tmp*3);
                q.add(tmp*5);
            }
            return q.poll().intValue();
        }
    }

    方法二:

    public class Solution {
        public int nthUglyNumber(int n) {
            if(n == 1) {
                return 1;
            }
            ArrayList<Integer> list = new ArrayList<Integer>();
            int a = 0, b = 0, c = 0;
            list.add(1);
            for(int i = 1; i < n; i++) {
                int nextVal = Math.min(Math.min(list.get(a) * 2, list.get(b) * 3), list.get(c) * 5);
                list.add(nextVal);
                if(nextVal == list.get(a) * 2) {
                    a++;
                }
                if(nextVal == list.get(b) * 3) {
                    b++;
                }
                if(nextVal == list.get(c) * 5) {
                    c++;
                }
            }
            return list.get(n-1);
        }
    }

    Reference:

    1. https://leetcode.com/discuss/59825/java-solution-using-priorityqueue

    2. https://leetcode.com/discuss/55304/java-easy-understand-o-n-solution

     

  • 相关阅读:
    八皇后-递归
    代码复用3
    权限管理系统-角色组模块
    MzBlog分析
    linux shell 终端中文乱码(转)
    LINUX下中文语言包的安装(转)
    每一个程序员必须知道的业内英语词汇(转)
    80后创业故事之:兄弟散伙,创业失败(转)
    尊重用户的习惯审美,不要挑战用户的习惯(转)
    libpcre.so.1 cannot be found
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4896565.html
Copyright © 2020-2023  润新知