码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 263: Ugly Number

时间:2017-12-06 14:35:22      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:ogr   hat   positive   nbsp   other   color   als   actor   style   

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

标签:ogr   hat   positive   nbsp   other   color   als   actor   style   

原文地址:http://www.cnblogs.com/liangmou/p/7992057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!