leetcode 263 ugly number

Ugly number? Ugly numbers? No!

Definition of the number of ugly
humble number is a positive integer that contains only the prime factors 2, 3, 5.

As the name suggests is that all factors of2, 3, 5 composition

Sample

Input: 8
Output: true
interpretation: 8 = 2 × 2 × 2

Resolve

Factor 8 comprising 1, 2, 4
Factor 4 is 1, 2

in conclusion

Number is a number of ugly all its factors are the number of ugly
useRecursionJudge

code show as below:

class Solution {
public:
    bool isUgly(int num) {
        if (num == 0) return false;
        if (num == 1) return true;       //说明是丑数
        if (num % 2 == 0) {
            num = num / 2;
            return isUgly(num);
        }
        if (num % 3 == 0) {
            num = num / 3;
            return isUgly(num);
        }
        if (num % 5 == 0) {
            num = num / 5;
            return isUgly(num);
        }
        return false;
    }
};
Published 34 original articles · won praise 0 · Views 593

Guess you like

Origin blog.csdn.net/Luyoom/article/details/103571327