mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +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,28 +7,24 @@
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* 键值对 int->string */
|
||||
class Pair {
|
||||
public int key;
|
||||
public string val;
|
||||
public Pair(int key, string val) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
class Pair(int key, string val) {
|
||||
public int key = key;
|
||||
public string val = val;
|
||||
}
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
private readonly List<Pair?> buckets;
|
||||
List<Pair?> buckets;
|
||||
public ArrayHashMap() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
buckets = new();
|
||||
buckets = [];
|
||||
for (int i = 0; i < 100; i++) {
|
||||
buckets.Add(null);
|
||||
}
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
private int HashFunc(int key) {
|
||||
int HashFunc(int key) {
|
||||
int index = key % 100;
|
||||
return index;
|
||||
}
|
||||
@@ -57,7 +53,7 @@ class ArrayHashMap {
|
||||
|
||||
/* 获取所有键值对 */
|
||||
public List<Pair> PairSet() {
|
||||
List<Pair> pairSet = new();
|
||||
List<Pair> pairSet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
pairSet.Add(pair);
|
||||
@@ -67,7 +63,7 @@ class ArrayHashMap {
|
||||
|
||||
/* 获取所有键 */
|
||||
public List<int> KeySet() {
|
||||
List<int> keySet = new();
|
||||
List<int> keySet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
keySet.Add(pair.key);
|
||||
@@ -77,7 +73,7 @@ class ArrayHashMap {
|
||||
|
||||
/* 获取所有值 */
|
||||
public List<string> ValueSet() {
|
||||
List<string> valueSet = new();
|
||||
List<string> valueSet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
valueSet.Add(pair.val);
|
||||
|
||||
Reference in New Issue
Block a user