![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构算法
诗远
Unity,Cocos程序 游戏 开发。
喜欢折腾,软路由,nas,电视盒子。欢迎大家相互交流问题。
展开
-
C# dictionary 自定义类型的containkey 方法实现
C# dictionary 自定义类型的containkey 方法实现https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2参考博客https://blog.csdn.net/ghhong1988/article/details/110469145public class myVectorEqualityComparer : IEquali原创 2022-03-11 09:37:48 · 496 阅读 · 0 评论 -
C# 实现红黑树
public class RBTree<K, V> : Map<K, V> where K : System.IComparable<K>{ private const bool RED = true; private const bool BLACK = false; public class Node { public K key; public V value; public N..原创 2021-05-16 23:32:41 · 534 阅读 · 0 评论 -
C# 实现并查集
路径压缩原创 2021-05-10 19:07:06 · 475 阅读 · 0 评论 -
C#实现 Trie 字典树公共前缀
public interface Map<K, V>{ void add(K key, V value); V remove(K key); bool contains(K key); V get(K key); void set(K key, V value); int getSize(); bool isEmpty();}public class BSTMap<K, V> : Map<K, V> wher.原创 2021-05-09 16:52:07 · 346 阅读 · 0 评论 -
C# 实现 基于二叉树 BSTMap 的映射(Map)实现
public interface Map<K, V>{ void add(K key, V value); V remove(K key); bool contains(K key); V get(K key); void set(K key, V value); int getSize(); bool isEmpty();}public class BSTMap<K, V> : Map<K, V> wh.原创 2021-05-09 16:48:43 · 454 阅读 · 0 评论 -
C# 实现 基于链表 LinkedListMap 的映射(Map)实现
public interface Map<K, V>{ void add(K key, V value); V remove(K key); bool contains(K key); V get(K key); void set(K key, V value); int getSize(); bool isEmpty();}public class LinkedListMap<K, V> : Map<K, V.原创 2021-05-09 16:46:39 · 454 阅读 · 0 评论 -
C# 实现 LeetCode 677. 键值映射
废了好大劲 用的二叉树结构 不会使用迭代器,就用Dictionary模拟了一下迭代器public interface Map<K, V>{ void add(K key, V value); V remove(K key); bool contains(K key); V get(K key); void set(K key, V value); int getSize(); bool isEmpty();}...原创 2021-05-09 15:34:59 · 300 阅读 · 0 评论 -
C# 实现LeetCode 307. 区域和检索 - 数组可修改
public class NumArray{ public interface Merger<T> { T morge(T a, T b); } public class SegmentTree<T> { private T[] data; public T[] tree; private Merger<T> merger; public Se..原创 2021-05-07 00:09:27 · 256 阅读 · 0 评论 -
C# 实现 Leetcode203. 移除链表元素
创建一个虚拟头结点 从虚拟头结点去遍历public class Solution{ public ListNode RemoveElements(ListNode head, int val) { ListNode dummyHead = new ListNode(-1); dummyHead.next = head; ListNode prev = dummyHead; while (prev.next != ...原创 2021-05-04 11:51:45 · 311 阅读 · 0 评论 -
C# 实现单向链表
public class LinkedList<T>{ private class Node { public T data; public Node next; public Node(T data, Node next) { this.data = data; this.next = next; } public Node(T .原创 2021-05-04 10:35:56 · 377 阅读 · 0 评论 -
C# 实现线段树
线段树 第n层 的数量 2^(n-1)前n层的数量 2^n -1如果一个数组中有n个元素 存放在线段树中 开创 数组的大小应该为4npublic interface Merger<T>{ T morge(T a, T b);}public class SegmentTree<T>{ private T[] data; public T[] tree; private Merger<T> merger; ...原创 2021-04-20 19:28:50 · 372 阅读 · 0 评论 -
C# 实现希尔排序
public class ShellSort{ private ShellSort() { } public static void Sort<T>(T[] data) where T : System.IComparable<T> { int h = data.Length / 2; while (h >= 1) { for (var start = 0; start <.原创 2021-04-16 23:12:16 · 333 阅读 · 0 评论 -
C#实现冒泡排序
using System;public class BubbleSort{ private BubbleSort() { } public static void Sort<T>(T[] data) where T : IComparable { for (var i = 0; i < data.Length;) { int lastSwapIndex = 0; /.原创 2021-04-04 00:17:54 · 301 阅读 · 0 评论 -
C# PriorityQueue 优先队列
using System;using System.Collections.Generic;/// <summary>/// 最大堆 优先级/// </summary>/// <typeparam name="T"></typeparam>public class MaxHeap<T> where T : IComparable<T>{ public List<T> data; publ.原创 2021-04-03 22:36:03 · 1244 阅读 · 0 评论 -
C# 实现 215. 数组中的第K个最大元素
使用双路快速排序实现public class Solution { public int FindKthLargest(int[] nums, int k) { System.Random random = new System.Random(); return selectK(nums, 0, nums.Length , random, nums.Length - k); } public int selectK(int[] arr, in.原创 2021-04-03 22:24:52 · 322 阅读 · 0 评论 -
C# 实现 剑指 Offer 40. 最小的k个数
快速排序 求得结果public class Solution { public int[] GetLeastNumbers(int[] arr, int k) { if (k == 0) return new int[0] { }; System.Random random = new System.Random(); selectK(arr, 0, arr.Length - 1, random, k); int[] .原创 2021-04-03 22:03:58 · 301 阅读 · 0 评论 -
C# 实现最小堆
using System;using System.Collections.Generic;/// <summary>/// 最大堆 优先级/// </summary>/// <typeparam name="T"></typeparam>public class MinHeap<T> where T : IComparable<T>{ public List<T> data; publ.原创 2021-04-03 21:12:54 · 344 阅读 · 0 评论 -
C# 实现最大堆 用于优先级排序 堆排序
using System;using System.Text;namespace myCode{ class Program { static void Main(string[] args) { //回文.LongestPalindrome("mmmmmmm"); // XXCZ(); //TestNumber(); // TestGenerate().原创 2021-04-02 00:15:49 · 386 阅读 · 0 评论 -
C# 实现二分查找法 二分搜索树
using System.Collections;using System.Collections.Generic;using System.Text;/// <summary>/// 二分搜索树/// </summary>public class BST<T> where T : System.IComparable<T>{ public class Node { public T data; ..原创 2021-04-03 19:43:04 · 425 阅读 · 0 评论 -
C# 实现快速排序 双路快速排序 三路快速排序
从左向右依次递归 如果 数组中的元素都相等,就会变成 n²级别的复杂度算法public class QuickSort{ private QuickSort() { } public static void Sort<T>(T[] arr) where T : System.IComparable<T> { Sort(arr, 0, arr.Length - 1 , new System.Random() ); }原创 2021-02-16 16:47:47 · 272 阅读 · 0 评论 -
C#实现 剑指 Offer 51. 数组中的逆序对
题目https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/归并排序参考https://mp.csdn.net/editor/html/112734224代码public class Solution{ public int res = 0; public int ReversePairs(int[] nums) { res = 0; int[] ..原创 2021-02-15 13:53:50 · 223 阅读 · 0 评论 -
C#如何测试代码运行时间
方法1System.Diagnostics.Stopwatch stopwatch = new Stopwatch();stopwatch.Start(); // 开始监视代码运行时间// 需要测试的代码 ....stopwatch.Stop(); // 停止监视TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间double hours = timespan.TotalHours; // 总小时double minute原创 2021-01-03 13:48:59 · 1550 阅读 · 0 评论 -
C#单项链表
using System;namespace 链表{ //单链表结点类,采用泛型 public class Node<T> { private T data; //数据域,当前结点的数据 private Node<T> next; //引用域,即下一结点 //构造器:数据域+引用域,普通结点 public Node(T item, Node<T> p) { .原创 2020-12-07 09:51:11 · 301 阅读 · 0 评论 -
C#常见算法
一、求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m //方法一,通过bool值 static int F1(int m) { int sum = 0; bool flag = true; for (int i = 1; i <= m; i++) { if (flag) //一次是默认是True,下下也为True sum +=原创 2020-12-04 10:31:39 · 1040 阅读 · 0 评论 -
C# 实现选择排序
实现方式。using System;public class SelectionSort{ private SelectionSort() { } public static void Sort<T>(T[] arr) where T : IComparable<T> { for (var i = 0; i < arr.Length; i++) { int...原创 2021-02-09 13:26:52 · 255 阅读 · 0 评论 -
C# 实现插入排序
插入排序using System;public class InsertionSort{ private InsertionSort() { } public static void Sort<T>(T[] data) where T : IComparable<T> { for (var i = 0; i < data.Length; i++) { for (var j = i;原创 2021-02-09 11:46:23 · 601 阅读 · 0 评论 -
C# 实现归并算法
源码using System;using System.Text;public class 归并{ private 归并() { } private static int depth = 0; public static void Sort<T>(T[] arr) where T : IComparable<T> { Sort(arr, 0, arr.Length - 1); depth.原创 2021-01-17 11:38:44 · 324 阅读 · 0 评论