1. 程式人生 > >Leetcode 263: Ugly Number

Leetcode 263: Ugly Number

cto rime public hose urn return false solution who

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