264. Ugly Number II

  • 32.0%

https://leetcode.com/problems/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, and n does not exceed 1690.

Hint:

  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
  4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 2, L2 3, L3 * 5).

方法一:

my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int nthUglyNumber(int n) {
if(n==1) return 1;
vector<int> nums = {1};
int two=0, three=0, five=0;
int i=1;
while(i<n){
int cur = min(nums[five]*5, min(nums[three]*3, nums[two]*2));
nums.push_back(cur);
if(cur==nums[two]*2)
two++;
if(cur==nums[three]*3)
three++;
if(cur==nums[five]*5)
five++;
i++;
}
return nums[n-1];
}
};

我的代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int nthUglyNumber(int n) {
vector<int> res(n, 1);
int i2=0, i3=0, i5=0;
for(int i=1; i<n; i++){
int cur = min(res[i2]*2, min(res[i3]*3, res[i5]*5));
res[i] = cur;
if(res[i2]*2==cur) i2++;
if(res[i3]*3==cur) i3++;
if(res[i5]*5==cur) i5++;
}
return res[n-1];
}
};

方法二:

36ms, 28.93%, May.3rd, 2016

https://leetcode.com/discuss/58186/elegant-c-solution-o-n-space-time-with-detailed-explanation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int nthUglyNumber(int n) {
vector <int> results(1, 1);
int i = 0, j = 0, k = 0;
while(results.size() < n){
results.push_back(min(results[i] * 2, min(results[j] * 3, results[k] * 5)));
if(results.back() == results[i] * 2) ++i;
if(results.back() == results[j] * 3) ++j;
if(results.back() == results[k] * 5) ++k;
}
return results.back();
}
};

cpp


20ms, 44.77%, May.3rd, 2016

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int nthUglyNumber(int n) {
if(n <= 0) return false; // get rid of corner cases
if(n == 1) return true; // base case
int t2 = 0, t3 = 0, t5 = 0; //pointers for 2, 3, 5
vector<int> k(n);
k[0] = 1;
for(int i = 1; i < n ; i ++)
{
k[i] = min(k[t2]*2,min(k[t3]*3,k[t5]*5));
if(k[i] == k[t2]*2) t2++;
if(k[i] == k[t3]*3) t3++;
if(k[i] == k[t5]*5) t5++;
}
return k[n-1];
}
};

20ms, 44.77%, May.3rd, 2016

https://leetcode.com/discuss/53225/c-one-pass-simple-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int nthUglyNumber(int n) {
if(n<1) return 0;
int id2 = 0, id3 = 0, id5 = 0, rst = 1;
vector<int> buf;

while(--n){
buf.push_back(rst);
int v2 = 2 * (buf[id2]), v3 = 3 * (buf[id3]), v5 = 5 * (buf[id5]);
rst = min(v2, min(v3, v5));
id2 += (rst == v2), id3 += (rst == v3), id5 += (rst == v5);
}
return rst;
}
};

python

212ms, 67.75%, May.3rd, 2016

https://leetcode.com/discuss/57156/my-expressive-python-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
ugly = [1]
i2, i3, i5 = 0, 0, 0
while n > 1:
u2, u3, u5 = 2 * ugly[i2], 3 * ugly[i3], 5 * ugly[i5]
umin = min((u2, u3, u5))
if umin == u2:
i2 += 1
if umin == u3:
i3 += 1
if umin == u5:
i5 += 1
ugly.append(umin)
n -= 1
return ugly[-1]

java

The idea of this solution is from this page:http://www.geeksforgeeks.org/ugly-numbers/

The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:

1
2
3
(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …

We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5.

Then we use similar merge method as merge sort, to get every ugly number from the three subsequence.

Every step we choose the smallest one, and move one step after,including nums with same value.

Thanks for this author about this brilliant idea. Here is my java solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public int nthUglyNumber(int n) {
int[] ugly = new int[n];
ugly[0] = 1;
int index2 = 0, index3 = 0, index5 = 0;
int factor2 = 2, factor3 = 3, factor5 = 5;
for(int i=1;i<n;i++){
int min = Math.min(Math.min(factor2,factor3),factor5);
ugly[i] = min;
if(factor2 == min)
factor2 = 2*ugly[++index2];
if(factor3 == min)
factor3 = 3*ugly[++index3];
if(factor5 == min)
factor5 = 5*ugly[++index5];
}
return ugly[n-1];
}
}

my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public int nthUglyNumber(int n) {
int i=0, j=0, k=0;
int[] ugly = new int[n];
ugly[0] = 1;
int idx=0;
for(int l=1; l<n; l++){
int min = Math.min(ugly[i]*2, Math.min(ugly[j]*3, ugly[k]*5));
ugly[l] = min;
if(min==ugly[i]*2)
i++;
if(min==ugly[j]*3)
j++;
if(min==ugly[k]*5)
k++;
}
return ugly[n-1];
}
}
谢谢你,可爱的朋友。