-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumSolution.java
More file actions
50 lines (35 loc) · 1.35 KB
/
NumSolution.java
File metadata and controls
50 lines (35 loc) · 1.35 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
package bit;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by bruce_shan on 2018/6/13 15:44.
* Description : 各种数的求解算法
*/
public class NumSolution {
public int GetUglyNumber_Solution(int index) {
if(index<1) return 0;
if(index==1) return 1;
Queue<Integer> queue2 = new LinkedList<>(); // 2质数因子序列
Queue<Integer> queue3 = new LinkedList<>(); // 3质数因子序列
Queue<Integer> queue5 = new LinkedList<>(); // 5质数因子序列
int count = 1;
int current =1; // 丑数种子
while (count<index){
// 种子生成新的丑数
queue2.add(current*2);
queue3.add(current*3);
queue5.add(current*5);
// 只需要比较 三个序列中最小的值即可,而最小的值就是最先放进去的值
current = Math.min(Math.min(queue2.peek(),queue3.peek()),queue5.peek());
if(queue2.peek() == current ) queue2.poll();
if(queue3.peek() == current ) queue3.poll();
if(queue5.peek() == current ) queue5.poll();
count++;
}
return current;
}
public static void main(String[] args) {
NumSolution solution = new NumSolution();
System.out.println(solution.GetUglyNumber_Solution(10));
}
}