[LeetCode] Ugly Number (A New Question Added Today)

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.

这是今天才加上去的新题。没啥可说的。只要把ugly number的定义搞清楚就可以很好的写了。

因为prime factor只能为2,3,5。所以只要一个数不能被分解为只含有这三个数的乘式那么它就不是一个ugly number了。

代码如下。~

public class Solution {
    public boolean isUgly(int num) {
        if(num<=0){
            return false;
        }
        if(num==1){
            return true;
        }
        while(num!=1){
            if(num%2==0){
                num=num/2;
            }else if(num%3==0){
                num=num/3;
            }else if(num%5==0){
                num=num/5;
            }else{
                return false;
            }
        }
        return true;
    }
}
时间: 2024-10-28 16:04:55

[LeetCode] Ugly Number (A New Question Added Today)的相关文章

[LeetCode] Ugly Number II (A New Question Added Today)

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 as an

leetcode Ugly Number II

题目连接 https://leetcode.com/problems/ugly-number-ii/ Ugly Number II Description 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

[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 treated as an

LeetCode——Ugly Number

Description: 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

[leetcode]Ugly Number 解题报告 C语言

[题目] 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

LeetCode() Ugly Number II 背下来!

一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.size() < n) { results.push_back(min(results[i] * 2, min(results[j] * 3, results[k] * 5))); if (results.back() == results[i] * 2) ++i; if (results.back()

[LeetCode] Add Digits (a New question added)

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it without any

【13_263】Ugly Number

简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: Easy 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

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,