263. Ugly Number

原题链接:https://leetcode.com/problems/ugly-number/description/
实现如下:

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.isUgly(6));
        System.out.println(s.isUgly(14));
    }

    /**
     * 很简单的一道题目,我可能是刚睡醒的原因,竟然都没想出来。然后,就直接看了《剑指 Offer》上面的解析!
     * @param num
     * @return
     */
    public boolean isUgly(int num) {
        if (num < 1) {
            return false;
        }
        while (num % 2 == 0) {
            num /= 2;
        }
        while (num % 3 == 0) {
            num /= 3;
        }
        while (num % 5 == 0) {
            num /= 5;
        }
        return num == 1;
    }

}
posted @ 2018-04-06 17:42  optor  阅读(129)  评论(0编辑  收藏  举报