Leetcode 263: Ugly Number

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 263: Ugly Number相关的知识,希望对你有一定的参考价值。

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. 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.

 

 1 public class Solution {
 2     public bool IsUgly(int num) {
 3         if (num < 1) return false;
 4         
 5         while (num > 1)
 6         {
 7             if (num % 2 == 0)
 8             {
 9                 num /= 2;
10             }
11             else if (num % 3 == 0)
12             {
13                 num /= 3;
14             }
15             else if (num % 5 == 0)
16             {
17                 num /= 5;
18             }
19             else
20             {
21                 return false;
22             }
23         }
24         
25         return true;
26     }
27 }

 

以上是关于Leetcode 263: Ugly Number的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode263——Ugly Number

Leetcode 263: Ugly Number

LeetCode263. Ugly Number

LeetCode 263 Ugly Number

[leetcode-263-Ugly Number]

leetcode 263. Ugly Number