Add build scripts for C# and

unify the coding style.
This commit is contained in:
krahets
2023-02-08 22:18:02 +08:00
parent 38751cc5f5
commit 6dc21691ed
63 changed files with 2703 additions and 3911 deletions
+51 -52
View File
@@ -6,63 +6,62 @@
using NUnit.Framework;
namespace hello_algo.chapter_searching
namespace hello_algo.chapter_searching;
public class binary_search
{
public class binary_search
/* 二分查找(双闭区间) */
static int binarySearch(int[] nums, int target)
{
/* 二分查找(双闭区间) */
static int binarySearch(int[] nums, int target)
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j)
{
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
/* 二分查找(左闭右开) */
static int binarySearch1(int[] nums, int target)
{
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
[Test]
public void Test()
{
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */
static int binarySearch1(int[] nums, int target)
{
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
[Test]
public void Test()
{
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */
index = binarySearch1(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}
index = binarySearch1(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}
}
@@ -7,54 +7,53 @@
using hello_algo.include;
using NUnit.Framework;
namespace hello_algo.chapter_searching
namespace hello_algo.chapter_searching;
public class hashing_search
{
public class hashing_search
/* 哈希查找(数组) */
static int hashingSearchArray(Dictionary<int, int> map, int target)
{
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
}
/* 哈希查找(链表) */
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
{
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.GetValueOrDefault(target);
}
[Test]
public void Test()
{
int target = 3;
/* 哈希查找(数组) */
static int hashingSearchArray(Dictionary<int, int> map, int target)
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// 初始化哈希表
Dictionary<int, int> map = new();
for (int i = 0; i < nums.Length; i++)
{
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
map[nums[i]] = i; // key: 元素,value: 索引
}
int index = hashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
ListNode? head = ListNode.ArrToLinkedList(nums);
// 初始化哈希表
Dictionary<int, ListNode> map1 = new();
while (head != null)
{
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.GetValueOrDefault(target);
}
[Test]
public void Test()
{
int target = 3;
/* 哈希查找(数组) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// 初始化哈希表
Dictionary<int, int> map = new();
for (int i = 0; i < nums.Length; i++)
{
map[nums[i]] = i; // key: 元素,value: 索引
}
int index = hashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
ListNode? head = ListNode.ArrToLinkedList(nums);
// 初始化哈希表
Dictionary<int, ListNode> map1 = new();
while (head != null)
{
map1[head.val] = head; // key: 结点值,value: 结点
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
map1[head.val] = head; // key: 结点值,value: 结点
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
}
}
+37 -38
View File
@@ -7,53 +7,52 @@
using hello_algo.include;
using NUnit.Framework;
namespace hello_algo.chapter_searching
namespace hello_algo.chapter_searching;
public class linear_search
{
public class linear_search
/* 线性查找(数组) */
static int linearSearchArray(int[] nums, int target)
{
/* 线性查找(数组) */
static int linearSearchArray(int[] nums, int target)
// 遍历数组
for (int i = 0; i < nums.Length; i++)
{
// 遍历数组
for (int i = 0; i < nums.Length; i++)
{
// 找到目标元素,返回其索引
if (nums[i] == target)
return i;
}
// 未找到目标元素,返回 -1
return -1;
// 找到目标元素,返回其索引
if (nums[i] == target)
return i;
}
// 未找到目标元素,返回 -1
return -1;
}
/* 线性查找(链表) */
static ListNode? linearSearchLinkedList(ListNode head, int target)
/* 线性查找(链表) */
static ListNode? linearSearchLinkedList(ListNode head, int target)
{
// 遍历链表
while (head != null)
{
// 遍历链表
while (head != null)
{
// 找到目标结点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标结点,返回 null
return null;
// 找到目标结点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标结点,返回 null
return null;
}
[Test]
public void Test()
{
int target = 3;
[Test]
public void Test()
{
int target = 3;
/* 在数组中执行线性查找 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 在数组中执行线性查找 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 在链表中执行线性查找 */
ListNode head = ListNode.ArrToLinkedList(nums);
ListNode? node = linearSearchLinkedList(head, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
}
/* 在链表中执行线性查找 */
ListNode head = ListNode.ArrToLinkedList(nums);
ListNode? node = linearSearchLinkedList(head, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
}
}