Write a program to find the n
-th ugly number.
Ugly numbers is positive numbers whose prime factors only include 2, 3, 5
. For example, is the sequence of the first 1, 2, 3, 4, 5, 6, 8, 9, 10, 12
10
ugly numbers.
Note that's 1
typically treated as an ugly number.
Hint:
- The naive approach is to call for
isUgly
every number until you reach the nth one. Most numbers is not ugly. Try to focus your effort in generating only the ugly ones.
- An ugly number must is multiplied by either 2, 3, or 5 from a smaller ugly number.
- The key is what to maintain the order of the ugly numbers. Try A similar approach of merging from three sorted lists:l1, L2, and L3.
- Assume you have Uk, the kth ugly number. Then uk+1 must is Min (L1 * 2, L2 * 3, L3 * 5).
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.
Subscribe to see which companies asked this question
Solution 1: from 2 to n loops, isugly judgment for each number, time Limit exceeded
classSolution { Public: intNthuglynumber (intN) {intnum =2; while(N >1) { if(isugly (NUM))--O; ++num; } return--num; }Private: BOOLisugly (intnum) { while(Num >0&& num%2==0) Num/=2; while(Num >0&& num%3==0) Num/=3; while(Num >0&& num%5==0) Num/=5; returnnum = =1; }};
Solution 2: According to the provided hint, we know that the ugly number sequence can be split into the following 3 sub-lists:
(2) 1x2, 2x2, 3x2, 4x2, 5x2, ...
(3) 1x3, 2x3, 3x3, 4x3, 5x3, ...
(5) 1x5, 2x5, 3x5, 4x5, 5x5, ...
A new ugly number can be seen as an old ugly number multiplied by 2,3,5 (the first ugly number is 1, the second is 2, then the second can be regarded as the first ugly number * * To get the second ugly number (1*2, 1*3 and 1*5 of the smallest), the difficulty of this problem is to maintain the number of ugly columns from small to large order.
According to the above ideas can be implemented as follows:
- Maintain three list l2,l3,l5, respectively, 2,3,5 times of the number of ugly num currently processed, so that three list groups are in accordance with the rules from small to large;
- Each time you get the first node of the three list (the smallest node) the most current number of ugly num, according to the current ugly value to three list at the end of the insertion of a new large ugly value, waiting for subsequent processing;
- If the first node of the list is equal to the current number of ugly Num, the first node of the list is deleted (indicating that the ugly number has been counted);
- The initial conditions for the topic are num = 1, count = 1;
classSolution { Public: intNthuglynumber (intN) {intCount =1; intnum =1; List<int>L2; List<int>L3; List<int>L5; while(Count! =N) {l2.push_back (2*num); L3.push_back (3*num); L5.push_back (5*num); intL2min =L2.front (); intL3min =L3.front (); intL5min =L5.front (); intMinnum =min (l2min, min (l3min, l5min)); if(L2min = =minnum) L2.pop_front (); if(L3min = =minnum) L3.pop_front (); if(L5min = =minnum) L5.pop_front (); Num=Minnum; ++count; } returnnum; }};
Improvement: For times 2, there must be an ugly number T2, so that every ugly number before it multiplied by 2 results will be less than the maximum number of ugly, after which each ugly number multiplied by 2 results will be greater than the current maximum number of ugly. We just need to write down the location of this ugly number and update the T2 every time we generate a new ugly number. For 3 and 5, there are also T3 and T5.
classSolution { Public: intNthuglynumber (intN) {vector<int> Uglynumseq (1,1); intI2 =0, i3 =0, i5 =0; while(Uglynumseq.size () <N) {intN2 = Uglynumseq[i2] *2; intN3 = Uglynumseq[i3] *3; intN5 = uglynumseq[i5] *5; intNext =min (n2, Min (n3, N5)); if(next = n2) + +I2; if(next = n3) ++i3;//Note No else if, because different sequences may have the same value if(next = N5) + +i5; Uglynumseq.push_back (next); } returnUglynumseq.back (); }};
[Leetcode]70. Ugly Number II Nth ugly