Sword Finger offer-5th Chapter optimization of Time and space efficiency (number of ugly)

Source: Internet
Author: User

Title: We call the number of 2,3,5 that contain only the factor "the number of ugly." Looking for a 1500th ugly number. 1 is usually treated as the first ugly number.

Idea 1: The first step is to determine whether the ugly number: The ugly number is only the number of 2,3,5, so must be divisible by 2,3,5. By determining whether the remainder is zero, the entire number is known to be 1 by reducing the value of the entire number, except since it has been reduced. Returns TRUE.

The second step is to find the value of the nth ugly number, the disadvantage of which is that not the number of ugly numbers also require the remainder divisor of the operation, and therefore time-consuming. So we propose a space-for-time approach (who makes today's hardware updates fast). Like 2 ideas.

Idea 2: I use an array to store the ugly number in order from small to large. The last ugly number put in the array is the largest number of ugly m. Then, multiply the number of M before, 2,3,5 to find the next ugly number greater than M. Change the value of M to the last ugly number found. When 2 times the number of ugly before m, there must be an ugly number before it and 2 is multiplied by less than M, after which the number is greater than M. We can record this number. and update it. Can quickly find and 2 multiplied by more than m of that ugly number.

Java Code Idea 1:

//idea: The first step: Determine whether a number is an ugly number. Because the ugly number is only a good 2,3,5 of the number of factors, so we naturally think of an ugly number first to 2 to find the remainder if it is 0 and then the divisor to reduce the size of the number,//same for 3, and 5, and finally returns true if the result is 1. The second step is to count the value of the nth number of ugly numbers by judging whether it is an ugly number.  Public classFinduglynumber {//Judging whether a number is an ugly number     Public Booleanisugly (intNumber ) {        if(number<=0)            return false;  while(number%2==0) number/=2;  while(number%3==0) number/=3;  while(number%5==0) number/=5; return(number==1)?true:false; }     Public intUglynumber (intUglynumber) {        if(uglynumber<=0)            return0; intugly=0;//record the number of ugly numbers        intnumber=0;//Record numbers         while(ugly<Uglynumber) { number++; if(isugly (number)) ugly++; }        returnNumber ; }     Public Static voidMain (string[] args) {Finduglynumber finduglynumber=NewFinduglynumber (); intUglynumber=finduglynumber.uglynumber (3);    System.out.println (Uglynumber); }}

Java Code Idea 2:

//idea: Keep the uglynumber in the array in order from small to large. Because Uglynumber is a number that contains only the 2,3,5 factor,//Therefore, you can use an M to represent the largest number in the array, the last number in the array, and the previous ugly number by 2,3,5, and then preserving the decimal place greater than the M value. //as the next m. As we said before, every ugly number before m is multiplied by 2,3,5, in fact this can be determined to be multiplied by 2 o'clock in the presence of a certain value, before it the number is less than M//The number after it is greater than M.  Public classFindUglyNumber1 { Public intFinduglynumer (intindex) {       int[] uglynumber=New int[index]; uglynumber[0]=1; intNextnumber=1; intNextnumber2=0; intNextnumber3=0; intNextnumber5=0;  while(nextnumber<index) {           intMin=min (uglynumber[nextnumber2]*2,uglynumber[nextnumber3]*3,uglynumber[nextnumber5]*5); Uglynumber[nextnumber]=min;  while(uglynumber[nextnumber2]*2<=Uglynumber[nextnumber]) nextNumber2++;  while(uglynumber[nextnumber3]*3<=Uglynumber[nextnumber]) NextNumber3++;  while(uglynumber[nextnumber5]*5<=Uglynumber[nextnumber]) NextNumber5++; Nextnumber++; }       intUgly=uglynumber[nextnumber-1]; returnUgly; } Public intMinintIintJintk) {intMin=i<j?i:j; returnMin<k?min:k;} Public Static voidMain (string[] args) {FindUglyNumber1 finduglynumber=NewFindUglyNumber1 (); intUglynumber=finduglynumber.finduglynumer (3); System.out.println (Uglynumber);}}

Sword Finger offer-5th Chapter optimization of Time and space efficiency (number of ugly)

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.