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

思路:

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

代码:

class Solution {
public:
    int nthUglyNumber(int n)
    {
        if(n <= 0)
            return 0;
        int *uglyNum = new int[n]();
        int *uglyNum2 = uglyNum ;
        int *uglyNum3 = uglyNum ;
        int *uglyNum5 = uglyNum ;
        uglyNum[0] = 1;
        int count = 1;
        while(count < n)
        {
            int curUgly = min(min(*uglyNum2 * 2 , *uglyNum3 * 3) , *uglyNum5 * 5);
            uglyNum[count] = curUgly;
            while(*uglyNum2 * 2 <= curUgly)
                uglyNum2++;
            while(*uglyNum3 * 3 <= curUgly)
                uglyNum3++;
            while(*uglyNum5 * 5 <= curUgly)
                uglyNum5++;
            count++;
        }
        int result = uglyNum[n-1];
        delete [] uglyNum;
        return result;

    }
};

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

时间: 2024-11-08 06:52:22

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

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 (丑数)

题目: 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 i

[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 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 OJ:Happy Number(欢乐数)

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

LeetCode OJ:Missing Number (丢失的数)

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi

Leetcode-5198 Ugly Number III(丑数 III)

1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 #define _rep(i,a,b) for(int i = (a);i > b;i --) 3 #define INF 0x3f3f3f3f 4 #define MOD 1000000007 5 #define pb push_back 6 #define maxn 100003 7 typedef long long ll; 8 class Solution 9 { 10 public

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