mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 09:36:28 +08:00
feat: finalize ruby code transpilation (#1379)
This commit is contained in:
parent
a14be17b74
commit
a704c0d7f2
1 changed files with 14 additions and 14 deletions
|
@ -122,17 +122,17 @@
|
||||||
Queue<Integer> minHeap = new PriorityQueue<>();
|
Queue<Integer> minHeap = new PriorityQueue<>();
|
||||||
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
||||||
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
||||||
|
|
||||||
/* 元素入堆 */
|
/* 元素入堆 */
|
||||||
maxHeap.offer(1);
|
maxHeap.offer(1);
|
||||||
maxHeap.offer(3);
|
maxHeap.offer(3);
|
||||||
maxHeap.offer(2);
|
maxHeap.offer(2);
|
||||||
maxHeap.offer(5);
|
maxHeap.offer(5);
|
||||||
maxHeap.offer(4);
|
maxHeap.offer(4);
|
||||||
|
|
||||||
/* 获取堆顶元素 */
|
/* 获取堆顶元素 */
|
||||||
int peek = maxHeap.peek(); // 5
|
int peek = maxHeap.peek(); // 5
|
||||||
|
|
||||||
/* 堆顶元素出堆 */
|
/* 堆顶元素出堆 */
|
||||||
// 出堆元素会形成一个从大到小的序列
|
// 出堆元素会形成一个从大到小的序列
|
||||||
peek = maxHeap.poll(); // 5
|
peek = maxHeap.poll(); // 5
|
||||||
|
@ -140,13 +140,13 @@
|
||||||
peek = maxHeap.poll(); // 3
|
peek = maxHeap.poll(); // 3
|
||||||
peek = maxHeap.poll(); // 2
|
peek = maxHeap.poll(); // 2
|
||||||
peek = maxHeap.poll(); // 1
|
peek = maxHeap.poll(); // 1
|
||||||
|
|
||||||
/* 获取堆大小 */
|
/* 获取堆大小 */
|
||||||
int size = maxHeap.size();
|
int size = maxHeap.size();
|
||||||
|
|
||||||
/* 判断堆是否为空 */
|
/* 判断堆是否为空 */
|
||||||
boolean isEmpty = maxHeap.isEmpty();
|
boolean isEmpty = maxHeap.isEmpty();
|
||||||
|
|
||||||
/* 输入列表并建堆 */
|
/* 输入列表并建堆 */
|
||||||
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
|
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
|
||||||
```
|
```
|
||||||
|
@ -337,7 +337,7 @@
|
||||||
max_heap.push(2);
|
max_heap.push(2);
|
||||||
max_heap.push(5);
|
max_heap.push(5);
|
||||||
max_heap.push(4);
|
max_heap.push(4);
|
||||||
|
|
||||||
/* 获取堆顶元素 */
|
/* 获取堆顶元素 */
|
||||||
let peek = max_heap.peek().unwrap(); // 5
|
let peek = max_heap.peek().unwrap(); // 5
|
||||||
|
|
||||||
|
@ -373,17 +373,17 @@
|
||||||
var minHeap = PriorityQueue<Int>()
|
var minHeap = PriorityQueue<Int>()
|
||||||
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
||||||
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }
|
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }
|
||||||
|
|
||||||
/* 元素入堆 */
|
/* 元素入堆 */
|
||||||
maxHeap.offer(1)
|
maxHeap.offer(1)
|
||||||
maxHeap.offer(3)
|
maxHeap.offer(3)
|
||||||
maxHeap.offer(2)
|
maxHeap.offer(2)
|
||||||
maxHeap.offer(5)
|
maxHeap.offer(5)
|
||||||
maxHeap.offer(4)
|
maxHeap.offer(4)
|
||||||
|
|
||||||
/* 获取堆顶元素 */
|
/* 获取堆顶元素 */
|
||||||
var peek = maxHeap.peek() // 5
|
var peek = maxHeap.peek() // 5
|
||||||
|
|
||||||
/* 堆顶元素出堆 */
|
/* 堆顶元素出堆 */
|
||||||
// 出堆元素会形成一个从大到小的序列
|
// 出堆元素会形成一个从大到小的序列
|
||||||
peek = maxHeap.poll() // 5
|
peek = maxHeap.poll() // 5
|
||||||
|
@ -391,13 +391,13 @@
|
||||||
peek = maxHeap.poll() // 3
|
peek = maxHeap.poll() // 3
|
||||||
peek = maxHeap.poll() // 2
|
peek = maxHeap.poll() // 2
|
||||||
peek = maxHeap.poll() // 1
|
peek = maxHeap.poll() // 1
|
||||||
|
|
||||||
/* 获取堆大小 */
|
/* 获取堆大小 */
|
||||||
val size = maxHeap.size
|
val size = maxHeap.size
|
||||||
|
|
||||||
/* 判断堆是否为空 */
|
/* 判断堆是否为空 */
|
||||||
val isEmpty = maxHeap.isEmpty()
|
val isEmpty = maxHeap.isEmpty()
|
||||||
|
|
||||||
/* 输入列表并建堆 */
|
/* 输入列表并建堆 */
|
||||||
minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))
|
minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))
|
||||||
```
|
```
|
||||||
|
@ -405,7 +405,7 @@
|
||||||
=== "Ruby"
|
=== "Ruby"
|
||||||
|
|
||||||
```ruby title="heap.rb"
|
```ruby title="heap.rb"
|
||||||
|
# Ruby 未提供内置 Heap 类
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
|
Loading…
Reference in a new issue