[leetcode 263 264]Ugly Number I II

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.

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

AC代码:

class Solution {
public:
    bool isUgly(int num) {
        if(num==0)
            return false;
        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
                break;
        }
        return num==1;
    }
};

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 ugly number.

当然是用I中的判断函数一个数字一个数字的判断也是可以的,但是时间效率就不高了

AC代码:

class Solution
{
public:

    int minNum(int x,int y)
    {
        return x<y?x:y;
    }

    int nthUglyNumber(int n)
    {
        if(n==0)
            return 0;
        int *count=new int[n];
        count[0]=1;
        int two=0;
        int three=0;
        int five=0;
        int sum=1;
        int min=0;
        while(sum<n)
        {
            min=minNum(minNum(count[two]*2,count[three]*3),count[five]*5);
            if(min>count[sum-1])
            {
                count[sum]=min;
                ++sum;
            }
            if(min==count[two]*2)
                ++two;
            else if(min==count[three]*3)
                ++three;
            else
                ++five;
        }
        return count[sum-1];
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 21:17:22

[leetcode 263 264]Ugly Number I II的相关文章

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

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

LeetCode(264):Ugly Number II

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

(medium)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, 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

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, 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 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

264. Ugly Number II java solutions

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

LeetCode(313):Super Ugly Number

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 sequenc

【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