-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthUglyNumber.java
More file actions
36 lines (30 loc) · 880 Bytes
/
Copy pathNthUglyNumber.java
File metadata and controls
36 lines (30 loc) · 880 Bytes
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
import java.util.ArrayList;
/**
* Leetcode problem #264, Ugly Number II
* https://leetcode.com/problems/ugly-number-ii/
*/
public class NthUglyNumber {
public static void main(String[] args) {
int n = 10;
System.out.println(nthUglyNumber(n));
}
public static int nthUglyNumber(int n) {
ArrayList<Integer> list = new ArrayList<>();
int two = 0, three = 0, five = 0;
list.add(1);
while (list.size() < n) {
int m2 = list.get(two) * 2;
int m3 = list.get(three) * 3;
int m5 = list.get(five) * 5;
int min = Math.min(m2, Math.min(m3, m5));
list.add(min);
if (min == m2)
two++;
if (min == m3)
three++;
if (min == m5)
five++;
}
return list.get(n - 1);
}
}