-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution264.java
More file actions
40 lines (37 loc) · 1.1 KB
/
Solution264.java
File metadata and controls
40 lines (37 loc) · 1.1 KB
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
import java.util.ArrayList;
import java.util.List;
/**
* Created by Alex on 2016/3/16.
*/
public class Solution264 {
public int nthUglyNumber(int n) {
List<Double> result = new ArrayList<>();
result.add(1.0);
for (int i=0; i<n; i++){
for(int j=0; j<3; j++){
double temp;
if(j == 0){
temp = result.get(i) * 2;
}else if(j == 1){
temp = result.get(i) * 3;
}else{
temp = result.get(i) * 5;
}
for(int k=i; k<result.size(); k++){
if(temp == result.get(k)){
break;
}
if(k == result.size()-1){
result.add(temp);
break;
}
if(temp > result.get(k) && temp < result.get(k+1)){
result.add(k+1, temp);
break;
}
}
}
}
return result.get(n-1).intValue();
}
}