-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution264.java
executable file
·35 lines (33 loc) · 977 Bytes
/
Solution264.java
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
import java.util.ArrayList;
public class Solution264 {
public int nthUglyNumber(int n) {
n = n-1;
ArrayList<Integer> init_ans = new ArrayList<>();
init_ans.add(1);
int i2 = 0;
int i3 = 0;
int i5 = 0;
for (int i =0;i<=n;i++){
int last_2 = init_ans.get(i2) * 2;
int last_3 = init_ans.get(i3)*3;
int last_5 = init_ans.get(i5)*5;
int minval = Math.min(last_2,Math.min(last_3,last_5));
init_ans.add(minval);
if(minval==last_2){
i2= i2+1;
}
if (minval==last_3){
i3 = i3+1;
}
if (minval==last_5){
i5 = i5+1;
}
}
System.out.println(init_ans);
return init_ans.get(n);
}
public static void main(String[] args) {
Solution264 s = new Solution264();
System.out.println(s.nthUglyNumber(10));
}
}