New Ket: Sword refers to offer: Ugly number (Java) __java

Source: Internet
Author: User

Cattle on the corresponding topic:

Analytical:

Method One:

To judge each integer is not an ugly number of solutions, intuitive but not efficient:

A number M is a factor of another number n, which means that n can be divisible by M, that is, n%m==0. According to the definition of ugliness, the number of ugliness can only be divisible by 2,3,5. That is, if a number is divisible by 2, we divide it continuously by 2, and if divisible by 3, divide it by 3, and if divisible by 5, divide by 5. If the last thing we get is 1, then this number is the ugly number, otherwise it is not. Method Two:

The next ugly number must be the existing ugly number multiplied by 2 or 3 or 5. The key to this kind of thinking is how to determine the number of ugly in the array is the sort of good. Assuming that there are already a number of ugly numbers in the array stored in the array, and the largest number of ugly numbers as M, we then analyze how to generate the next ugly number. The ugly number must be the result of a previous ugly number multiplied by the 2,3,5. So we're going to first consider multiplying each ugly number by 2. When multiplied by 2, a number of results that are less than or equal to M are obtained. Because it is generated in order, less than or equal to M is definitely already in the array, we don't need to think about it again; we'll get a number of results greater than m, but we only need the first result that is greater than M, because we want the ugly numbers to be generated in order of small to large, other larger results later. We take the first one multiplied by 2 and the result of greater than M is M2. Again, we multiply each ugly number by 3, 5, and get the first results greater than M M3 and M5. Then the next ugly number should be m2,m3,m5. The smallest of these 3 numbers.

In the previous analysis, we mentioned multiplying each ugly number by 2,3,5. In fact this is not necessary, because the existing ugly numbers are stored sequentially in the array. For times 2, there must be an ugly number of T2, every ugly number before it multiplied by 2 results will be less than the maximum number of ugly, after which every ugly number multiplied by 2 results will be too large. We just need to write down the position of the ugly number and update the T2 each time we generate a new ugly number. For times 3 and 5, there is also the same T3 and T5.

public class Solution {public
    int getuglynumber_solution (int index) {
        if (index <= 0) return
            0;
        int[] ugly = new Int[index];
        Ugly[0] = 1;
        int i2 = 0;
        int i3 = 0;
        int i5 = 0;
        for (int i = 1; i < index; i++) {
            int tmp = MIN (ugly[i2]*2,ugly[i3]*3,ugly[i5]*5);
            Ugly[i] = tmp;
            while (ugly[i2]*2 <= ugly[i])
                i2++;
            while (Ugly[i3]*3 <= ugly[i])
                i3++;
            while (Ugly[i5]*5 <= ugly[i])
                i5++;
        }
        return ugly[index-1];
    }
    public int min (int a,int b,int c) {
        if (a > B)
            a = B;
        if (a > c)
            a = C;
        return A;
    }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.