【LeetCode】Ugly Number解題レポート

1159 ワード

Ugly Number
[LeetCode]
https://leetcode.com/problems/ugly-number/
Total Accepted: 22335 Total Submissions: 67449 Difficulty: Easy
Question
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Examples
For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
Ways
方法1
この数のすべての2,3,5の因子をすべて除去し,残りの数を1とすると,すべてがこれらの因子で構成されていることを示す.
public static boolean isUgly(int num) {
    if (num == 1) {
        return true;
    }

    while (num >= 2 && num % 2 == 0) {
        num = num / 2;
    }
    while (num >= 3 && num % 3 == 0) {
        num = num / 3;
    }
    while (num >= 5 && num % 5 == 0) {
        num = num / 5;
    }
    return num == 1;
}

Solution
私のGitHubに預けられています.
https://github.com/fuxuemingzhu/UglyNumber
Captures
テスト結果のスクリーンショット:
Reference
http://blog.csdn.net/xudli/article/details/47786867
Date
2015/10/16 21:20:23