mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@ -7,13 +7,13 @@
|
||||
namespace hello_algo.chapter_heap;
|
||||
|
||||
public class heap {
|
||||
public void TestPush(PriorityQueue<int, int> heap, int val) {
|
||||
void TestPush(PriorityQueue<int, int> heap, int val) {
|
||||
heap.Enqueue(val, val); // 元素入堆
|
||||
Console.WriteLine($"\n元素 {val} 入堆后\n");
|
||||
PrintUtil.PrintHeap(heap);
|
||||
}
|
||||
|
||||
public void TestPop(PriorityQueue<int, int> heap) {
|
||||
void TestPop(PriorityQueue<int, int> heap) {
|
||||
int val = heap.Dequeue(); // 堆顶元素出堆
|
||||
Console.WriteLine($"\n堆顶元素 {val} 出堆后\n");
|
||||
PrintUtil.PrintHeap(heap);
|
||||
|
||||
@ -9,11 +9,11 @@ namespace hello_algo.chapter_heap;
|
||||
/* 大顶堆 */
|
||||
class MaxHeap {
|
||||
// 使用列表而非数组,这样无须考虑扩容问题
|
||||
private readonly List<int> maxHeap;
|
||||
List<int> maxHeap;
|
||||
|
||||
/* 构造函数,建立空堆 */
|
||||
public MaxHeap() {
|
||||
maxHeap = new List<int>();
|
||||
maxHeap = [];
|
||||
}
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
@ -130,7 +130,7 @@ public class my_heap {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化大顶堆 */
|
||||
MaxHeap maxHeap = new(new int[] { 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
|
||||
MaxHeap maxHeap = new([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
|
||||
Console.WriteLine("\n输入列表并建堆后");
|
||||
maxHeap.Print();
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ namespace hello_algo.chapter_heap;
|
||||
|
||||
public class top_k {
|
||||
/* 基于堆查找数组中最大的 k 个元素 */
|
||||
public static PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
|
||||
PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
|
||||
// 初始化小顶堆
|
||||
PriorityQueue<int, int> heap = new();
|
||||
// 将数组的前 k 个元素入堆
|
||||
@ -28,7 +28,7 @@ public class top_k {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 7, 6, 3, 2 };
|
||||
int[] nums = [1, 7, 6, 3, 2];
|
||||
int k = 3;
|
||||
PriorityQueue<int, int> res = TopKHeap(nums, k);
|
||||
Console.WriteLine("最大的 " + k + " 个元素为");
|
||||
|
||||
Reference in New Issue
Block a user