美文网首页
【剑指Offer 34】丑数

【剑指Offer 34】丑数

作者: 3e1094b2ef7b | 来源:发表于2017-07-22 09:35 被阅读13次

    题目:我们把只包含因子2、3 和5 的数称作丑数(Ugly Number)。求从小到大的顺序的第1500个丑数。

    代码如下:

    解法1:

    package demo;
    
    /**
     * 丑数
     * 
     * @author xiangdonglee
     *
     */
    public class Test34_1 {
        /**
         * 判断一个数是否是丑数(只有2、3、5因子)
         * 
         * @param num
         *            待判断的数,非负
         * @return true:是丑数;false:不是丑数
         */
        private static boolean isUgly(int num) {
            while (num % 2 == 0) {
                num /= 2;
            }
            while (num % 3 == 0) {
                num /= 3;
            }
            while (num % 5 == 0) {
                num /= 5;
            }
            return num == 1;
        }
    
        /**
         * 找第index个丑数
         * 
         * @param index
         *            第index个丑数
         * @return 对应的丑数值
         */
        public static int getUglyNumber1(int index) {
            if (index <= 0) {
                return 0;
            }
            int num = 0;
            int uglyFound = 0;
            while (uglyFound < index) {
                num++;
                if (isUgly(num)) {
                    uglyFound++;
                }
            }
            return num;
        }
    
        public static void main(String[] args) {
            System.out.println("Solution 1:");
            test1();
        }
    
        private static void test1() {
            System.out.println("第1个丑数:" + getUglyNumber1(1));
            System.out.println("第2个丑数:" + getUglyNumber1(2));
            System.out.println("第3个丑数:" + getUglyNumber1(3));
            System.out.println("第4个丑数:" + getUglyNumber1(4));
            System.out.println("第5个丑数:" + getUglyNumber1(5));
            System.out.println("第6个丑数:" + getUglyNumber1(6));
            System.out.println("第7个丑数:" + getUglyNumber1(7));
            System.out.println("第8个丑数:" + getUglyNumber1(8));
            System.out.println("第9个丑数:" + getUglyNumber1(9));
            System.out.println("第10个丑数:" + getUglyNumber1(10));
            System.out.println("第11个丑数" + getUglyNumber1(11));
            System.out.println("第1500个丑数" + getUglyNumber1(1500));
            System.out.println("第0个丑数" + getUglyNumber1(0));
        }
    }
    
    运行结果

    解法2:

    package demo;
    
    public class Test34_2 {
        /**
         * 找第index个丑数
         * 
         * @param index
         *            第index个丑数
         * @return 对应的丑数值
         */
        public static int getUglyNumber2(int index) {
            if (index <= 0) {
                return 0;
            }
            int[] pUglyNumbers = new int[index];
            pUglyNumbers[0] = 1;
            int nextUglyIndex = 1;
            int p2 = 0;
            int p3 = 0;
            int p5 = 0;
            while (nextUglyIndex < index) {
                int min = min(pUglyNumbers[p2] * 2, pUglyNumbers[p3] * 3, pUglyNumbers[p5] * 5);
                pUglyNumbers[nextUglyIndex] = min;
                while (pUglyNumbers[p2] * 2 <= pUglyNumbers[nextUglyIndex]) {
                    p2++;
                }
                while (pUglyNumbers[p3] * 3 <= pUglyNumbers[nextUglyIndex]) {
                    p3++;
                }
                while (pUglyNumbers[p5] * 5 <= pUglyNumbers[nextUglyIndex]) {
                    p5++;
                }
                nextUglyIndex++;
            }
            return pUglyNumbers[nextUglyIndex - 1];
        }
    
        private static int min(int i, int j, int k) {
            int min = i < j ? i : j;
            return min < k ? min : k;
        }
    
        public static void main(String[] args) {
            System.out.println("Solution 2:");
            test2();
        }
    
        private static void test2() {
            System.out.println("第1个丑数:" + getUglyNumber2(1));
            System.out.println("第2个丑数:" + getUglyNumber2(2));
            System.out.println("第3个丑数:" + getUglyNumber2(3));
            System.out.println("第4个丑数:" + getUglyNumber2(4));
            System.out.println("第5个丑数:" + getUglyNumber2(5));
            System.out.println("第6个丑数:" + getUglyNumber2(6));
            System.out.println("第7个丑数:" + getUglyNumber2(7));
            System.out.println("第8个丑数:" + getUglyNumber2(8));
            System.out.println("第9个丑数:" + getUglyNumber2(9));
            System.out.println("第10个丑数:" + getUglyNumber2(10));
            System.out.println("第11个丑数:" + getUglyNumber2(11));
            System.out.println("第1500个丑数:" + getUglyNumber2(1500));
            System.out.println("第0个丑数:" + getUglyNumber2(0));
        }
    }
    
    运行结果

    来源:http://blog.csdn.net/derrantcm/article/details/46753255

    相关文章

      网友评论

          本文标题:【剑指Offer 34】丑数

          本文链接:https://www.haomeiwen.com/subject/lmadkxtx.html