鱼C论坛

 找回密码
 立即注册
查看: 2307|回复: 0

[学习笔记] leetcode 263. Ugly Number

[复制链接]
发表于 2019-9-22 13:38:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Write a program to check whether a given number is an ugly number.

  2. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

  3. Example 1:

  4. Input: 6
  5. Output: true
  6. Explanation: 6 = 2 × 3
  7. Example 2:

  8. Input: 8
  9. Output: true
  10. Explanation: 8 = 2 × 2 × 2
  11. Example 3:

  12. Input: 14
  13. Output: false
  14. Explanation: 14 is not ugly since it includes another prime factor 7.
  15. Note:

  16. 1 is typically treated as an ugly number.
  17. Input is within the 32-bit signed integer range: [−231,  231 − 1].
复制代码

  1. class Solution {
  2.     public boolean isUgly(int num) {
  3.         if(num == 1) return true;
  4.         if(num == 0) return false;
  5.         int a = 2;
  6.         while(a != 0 && num % a == 0){
  7.             a = a * 2;
  8.         }
  9.         a = a /2 ;
  10.         if(a != 0)
  11.             num = num / a;
  12.         a =3;
  13.         while(a != 0 && num % a == 0){
  14.             a = a * 3;
  15.         }
  16.         
  17.         a = a /3;
  18.         // System.out.println(b);
  19.         if(a != 0)
  20.             num = num / a;
  21.         a = 5;
  22.         while(a != 0 && num % a == 0){
  23.             a = a * 5;
  24.         }
  25.         a = a /5;
  26.         if(a != 0)
  27.             num = num / a;
  28.         if(num == 1) return true;
  29.         else
  30.             return false;
  31.     }
  32. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 23:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表