lintcode:Ugly Number I

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 235. For example, 68 are ugly while 14 is not ugly since it includes another prime factor 7.

注意事项

Note that 1 is typically treated as an ugly number.

样例

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

解题

直接递归

public class Solution {
    /**
     * @param num an integer
     * @return true if num is an ugly number or false
     */
    public boolean isUgly(int num) {
        // Write your code here
        if(num <=0)
            return false;
        if(num == 1)
            return true;
        if(num %2 ==0)
            return isUgly(num/2);
        if(num %3 ==0)
            return isUgly(num/3);
        if(num%5 ==0)
            return isUgly(num/5);
        return false;

    }
}

while 循环‘

public class Solution {
    /**
     * @param num an integer
     * @return true if num is an ugly number or false
     */
    public boolean isUgly(int num) {
        // Write your code here
        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;

    }
}
时间: 2024-10-07 02:12:07

lintcode:Ugly Number I的相关文章

lintcode: ugly number

问题描述 Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the Kth ugly number. The first 5 ugly numbers are 3, 5, 7, 9, 15 - 问题分析 这个题,有多种方法求解,常见的是有辅助数据结构,比如priority_queue,或是常说的最大最小堆也可. 解法一:时间复杂度O(k log k),空间复杂度O(k) 使

[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

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