forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthUglyNumber.java
More file actions
54 lines (48 loc) · 1.45 KB
/
NthUglyNumber.java
File metadata and controls
54 lines (48 loc) · 1.45 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Week_02;
// 丑数
public class NthUglyNumber {
// 暴力循环判断,从1开始,能够只除尽2/3/5的数据则计数+1直至达到n ---超时
public int nthUglyNumber(int n) {
int cout = 1;
int t = 1;
while (cout < n) {
int temp = ++t;
while (temp > 1) {
if (temp % 2 == 0) {
temp = temp / 2;
} else if (temp % 3 == 0) {
temp = temp / 3;
} else if (temp % 5 == 0) {
temp = temp / 5;
} else {
temp = -1;
}
}
if (temp == 1) {
cout++;
}
}
return t;
}
// 动态规划 关键词---丑数只能整除2/3/5,则第n个丑数整除2/3/5是第【1,n-1】中的丑数
// 通过 2 ms 37.7 MB Java
public int nthUglyNumberOfPro(int n) {
int[] temp = new int[n];
temp[0] = 1;
int p2 = 0, p3 = 0, p5 = 0;
for (int i = 1; i < n; i++) {
int temp2 = temp[p2] * 2, temp3 = temp[p3] * 3, temp5 = temp[p5] * 5;
temp[i] = Math.min(Math.min(temp2, temp3), temp5);
if (temp[i] == temp2) {
p2++;
}
if (temp[i] == temp3) {
p3++;
}
if (temp[i] == temp5) {
p5++;
}
}
return temp[n - 1];
}
}