No.49-丑数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/89786178

1 丑数

在这里插入图片描述

public static int GetUglyNumber_Solution(int index) {

        if (index <= 0) {
            return 0;
        }

        int p2 = 0;
        int p3 = 0;
        int p5 = 0;
        int newNum = 1;

        ArrayList<Integer> list = new ArrayList<>();
        list.add(newNum);

        while (list.size() < index) {
            newNum = Math.min(Math.min(list.get(p2)*2, list.get(p3)*3), list.get(p5)*5);
            if (list.get(p2) * 2 == newNum) ++p2;
            if (list.get(p3) * 3 == newNum) ++p3;
            if (list.get(p5) * 5 == newNum) ++p5;

            list.add(newNum);
        }

        return list.get(index-1);

 
    }

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/89786178