• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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

recursively divide the number by 2,3,5 until finally reaches 1(true) or can't be divided by 2,3,5

 1 public class Solution {
 2     public static boolean isUgly(int num) {
 3         if (num <= 0) {
 4             return false;
 5         }
 6 
 7         int[] divisors = {2, 3, 5};
 8 
 9         for(int d : divisors) {
10             while (num % d == 0) {
11                 num /= d;
12             }
13         }
14         return num == 1;
15     }
16 }

 

posted @ 2015-12-23 06:59  neverlandly  阅读(232)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3