517. 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.

Example
Given num = 8 return true
Given num = 14 return false

Notice
Note that 1 is typically treated as an ugly number.


public class Solution {
    /**
     * @param num: An integer
     * @return: true if num is an ugly number or false
     */
    // 如果能整除2 3 5 就除一次,直到最后不能除
    public boolean isUgly(int num) {
        while(num >= 2) {
            if(num % 2 == 0) num /= 2;
            else if(num % 3 == 0) num /= 3;
            else if(num % 5 == 0) num /= 5;
            else
                return false;
        }
        return num == 1;
    }
}

原文地址:https://www.cnblogs.com/lawrenceSeattle/p/10285889.html

时间: 2024-11-05 19:04:32

517. Ugly Number的相关文章

ugly number

leetcode 263 & 264是关于ugly number的: https://leetcode.com/problems/ugly-number/ https://leetcode.com/problems/ugly-number-ii/ 简单的263,给定一个数,判断该数是否ugly:这个比较直观,只需要判断该数是否只能被2, 3, 5整除即可: public boolean isUgly(int num) {     if (num <= 0) {         return 

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

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, 14, 16, 19, 26, 28, 32] is the sequence of the first

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

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 263. Ugly Number JAVA语言

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

Leetcode264. Ugly Number II JAVA语言

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 263. Ugly Number

263. Ugly Number Total Accepted: 65061 Total Submissions:173755 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 example, 6, 8 are ugly

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,