[leetcode] 263. Ugly Number (easy)

只要存在一种因数分解后,其因子是2,3,5中的一种或多种,就算是ugly数字。

思路:

以2/3/5作为除数除后,最后结果等于1的就是ugly数字

Runtime: 4 ms, faster than 98.64% of C++ online submissions for Ugly Number.

class Solution
{
public:
  bool isUgly(int num)
  {
    while (num % 5 == 0 && num > 4)
      num /= 5;
    while (num % 3 == 0 && num > 2)
      num /= 3;
    while (num % 2 == 0 && num > 1)
      num /= 2;
    return num == 1;
  }
};

 

posted @ 2018-11-23 13:46  Ruohua3kou  阅读(86)  评论(0编辑  收藏  举报