-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp264.rs
34 lines (29 loc) · 914 Bytes
/
p264.rs
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
#[test]
fn test() {
use method1::nth_ugly_number;
assert_eq!(nth_ugly_number(10), 12);
assert_eq!(nth_ugly_number(1), 1);
}
// 优先队列,小顶堆
// 每次用最小的元素乘2、3、5。但要注意去重
// 堆用i64,不然会溢出,leetcode坑人的传统手艺
mod method1 {
pub fn nth_ugly_number(n: i32) -> i32 {
use std::cmp::Reverse;
use std::collections::BinaryHeap;
let mut heap: BinaryHeap<Reverse<i64>> = BinaryHeap::new();
let mut ans: i64 = 1;
heap.push(Reverse(ans));
for _ in 0..n {
heap.push(Reverse(ans * 2));
heap.push(Reverse(ans * 3));
heap.push(Reverse(ans * 5));
ans = heap.pop().unwrap().0;
// 去重
while !heap.is_empty() && ans == heap.peek().unwrap().0 {
heap.pop();
}
}
ans as i32
}
}