-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path264.ugly-number-ii.cpp
54 lines (52 loc) · 1.12 KB
/
264.ugly-number-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode id=264 lang=cpp
*
* [264] Ugly Number II
*
* https://leetcode.com/problems/ugly-number-ii/description/
*
* algorithms
* Medium (35.55%)
* Total Accepted: 104.9K
* Total Submissions: 288K
* Testcase Example: '10'
*
* Write a program to find the n-th ugly number.
*
* Ugly numbers are positive numbers whose prime factors only include 2, 3,
* 5.
*
* Example:
*
*
* Input: n = 10
* Output: 12
* Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10
* ugly numbers.
*
* Note:
*
*
* 1 is typically treated as an ugly number.
* n does not exceed 1690.
*
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int nthUglyNumber(int n) {
int i=0, j=0, k=0;
if(n == 1) return 1;
vector<long> vec(1, 1);
for(int z=2; z<=n; z++){
int m = min(vec[i]*2, min(vec[j]*3, vec[k]*5) );
if(z == n) return m;
vec.push_back(m);
if(m == vec[i]*2) i++;
if(m == vec[j]*3) j++;
if(m == vec[k]*5) k++;
}
return -1;
}
};