- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution264.java
More file actions
Latest commit
executable file
·35 lines (33 loc) · 977 Bytes
/
Copy pathSolution264.java
File metadata and controls
executable file
·35 lines (33 loc) · 977 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
importjava.util.ArrayList;
publicclassSolution264 {
publicintnthUglyNumber(intn) {
n = n-1;
ArrayList<Integer> init_ans = newArrayList<>();
init_ans.add(1);
inti2 = 0;
inti3 = 0;
inti5 = 0;
for (inti =0;i<=n;i++){
intlast_2 = init_ans.get(i2) * 2;
intlast_3 = init_ans.get(i3)*3;
intlast_5 = init_ans.get(i5)*5;
intminval = 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);
returninit_ans.get(n);
}
publicstaticvoidmain(String[] args) {
Solution264s = newSolution264();
System.out.println(s.nthUglyNumber(10));
}
}