Leetcode 263. Ugly Number

文章作者:Tyan
博客:noahsnail.com  |  CSDN  | 

1. Description

Ugly Number

2. Solution

class Solution {
public:
    bool isUgly(int num) {
        if(num <= 0) {
            return false;
        }
        if(num == 1) {
            return true;
        }
        while(num != 1) {
            if(num % 2 == 0) {
                num = num / 2;
            }
            else if(num % 3 == 0) {
                num = num / 3;
            }
            else if(num % 5 == 0) {
                num = num / 5;
            }
            else {
                return false;
            }
        }
        return true;
    }
};

Reference

  1. https://leetcode.com/problems/ugly-number/description/

你可能感兴趣的:(Leetcode 263. Ugly Number)