关闭

LeetCode 263 Ugly Number

标签: leetcode
370人阅读 评论(2) 收藏 举报
分类:

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.


题目链接:https://leetcode.com/problems/ugly-number/

题目分析:除除就好

public class Solution {
    public boolean isUgly(int num) {
        if(num == 0) {
            return false;
        }
        while(num % 2 == 0) {
            num /= 2;
        }
        while(num % 3 == 0) {
            num /= 3;
        }
        while(num % 5 == 0) {
            num /= 5;
        }
        return num == 1;
    }
}


0
0
查看评论

[Leetcode-263] Ugly Number(丑陋数)

【分析】思路:根据丑陋数的定义,我们将给定数除以2、3、5,直到无法整除,也就是除以2、3、5的余数不再为0时停止。这时如果得到1,说明是所有因子都是2或3或5,如果不是1,则不是丑陋数。
  • xy010902100449
  • xy010902100449
  • 2015-09-05 10:57
  • 2159

leetcode 263: Ugly Number

Ugly Number Total Accepted: 1849 Total Submissions: 5482 Write a program to check whether a given number is an ugly number. Ugly numbers are posi...
  • xudli
  • xudli
  • 2015-08-19 20:17
  • 5263

[Leetcode 264] Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example,&...
  • sbitswc
  • sbitswc
  • 2015-09-02 00:23
  • 2226

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....
  • pessis1
  • pessis1
  • 2016-11-29 18:59
  • 301

[leetcode] 264. Ugly Number II 解题报告

题目链接:https://leetcode.com/problems/ugly-number-ii/ Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime...
  • qq508618087
  • qq508618087
  • 2015-12-13 15:54
  • 518

leetCode #263 Ugly Number

题目:判断一个数是不是丑数:即质因数只有2 3 5组成 分析:那么丑数必为2^i*3^j*5^k 答案: class Solution { public: bool isUgly(int num) { if (num == 0) return f...
  • MarStarck
  • MarStarck
  • 2015-08-19 23:53
  • 183

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. ...
  • happyxuma1991
  • happyxuma1991
  • 2016-03-22 22:49
  • 61

leetcode: (263) Ugly Number

【Question】 Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only inclu...
  • zk031
  • zk031
  • 2015-08-25 08:57
  • 189

leetcode 263 Ugly Number

Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only includ...
  • zhutianyu_123
  • zhutianyu_123
  • 2015-08-31 11:42
  • 133

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....
  • suichen1
  • suichen1
  • 2016-06-26 18:50
  • 143
    个人资料
    • 访问:740070次
    • 积分:15305
    • 等级:
    • 排名:第880名
    • 原创:818篇
    • 转载:7篇
    • 译文:0篇
    • 评论:122条
    博客专栏