LeetCode OJ 之 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.

思路:

参考:http://blog.csdn.net/u012243115/article/details/45222269

代码:

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

版权声明:转载请注明出处。

时间: 2024-07-30 09:54:31

LeetCode OJ 之 Ugly Number (丑数)的相关文章

LeetCode OJ: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 ty

LeetCode OJ 之 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, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated a

Ugly number丑数2

[抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [总结]: [复杂度]:Time complexity: O() Space complexity: O() [英文数据结构,为什么不用别的数据结构]: 随意插入:pq heap,防止重复插入:hashmap queue是接口类,可以

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

leetcode笔记: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, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treat

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

lintcode 中等题:Ugly Numbers 丑数

题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n) 解题 法一:直接暴力,逐次判断一个数是不是丑数 下面只对其中的奇数判断是否是丑数,加不加奇数都超时. class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */

[LintCode] 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. Notice Note that

【Leetcode】Super Ugly Number

题目链接:https://leetcode.com/problems/super-ugly-number/ 题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13