-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetUglyNumber_Solution.java
More file actions
46 lines (42 loc) · 1.46 KB
/
GetUglyNumber_Solution.java
File metadata and controls
46 lines (42 loc) · 1.46 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
package com.company;
import org.junit.Test;
public class GetUglyNumber_Solution {
//把只包含质因子 2、3 和 5的数称作丑数(Ugly Number)
// 例如 6、8 都是丑数,但 14 不是,因为它包含质因子 7
// 习惯上我们把 1 当做是第一个丑数
// 求按从小到大的顺序的第 N 个丑数
public int GetUglyNumber_Solution(int index) {
if (index <= 0) {
return 0;
}
int[] pUglyNumbers = new int[index];
pUglyNumbers[0] = 1;
int nextUglyIndex = 1;
int p2 = 0;
int p3 = 0;
int p5 = 0;
while (nextUglyIndex < index) {
int min = min(pUglyNumbers[p2] * 2, pUglyNumbers[p3] * 3, pUglyNumbers[p5] * 5);
pUglyNumbers[nextUglyIndex] = min;
while (pUglyNumbers[p2] * 2 <= pUglyNumbers[nextUglyIndex]) {
p2++;
}
while (pUglyNumbers[p3] * 3 <= pUglyNumbers[nextUglyIndex]) {
p3++;
}
while (pUglyNumbers[p5] * 5 <= pUglyNumbers[nextUglyIndex]) {
p5++;
}
nextUglyIndex++;
}
return pUglyNumbers[nextUglyIndex - 1];
}
public int min(int n1, int n2, int n3) {
int min = n1 < n2 ? n1 : n2;
return min < n3 ? min : n3;
}
@Test
public void test() {
System.out.println(GetUglyNumber_Solution(2));
}
}