mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
Modify some formats.
This commit is contained in:
parent
0a0374efa0
commit
49756d8c7e
4 changed files with 36 additions and 22 deletions
|
@ -6,7 +6,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
{
|
||||
public class Array
|
||||
{
|
||||
//随机返回一个数组元素
|
||||
/// <summary>
|
||||
/// 随机返回一个数组元素
|
||||
/// </summary>
|
||||
public static int RandomAccess(int[] nums)
|
||||
{
|
||||
Random random = new();
|
||||
|
@ -15,7 +17,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
return randomNum;
|
||||
}
|
||||
|
||||
//扩展数组长度
|
||||
/// <summary>
|
||||
/// 扩展数组长度
|
||||
/// </summary>
|
||||
public static int[] Extend(int[] nums, int enlarge)
|
||||
{
|
||||
// 初始化一个扩展长度后的数组
|
||||
|
@ -29,7 +33,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
return res;
|
||||
}
|
||||
|
||||
//在数组的索引 index 处插入元素 num
|
||||
/// <summary>
|
||||
/// 在数组的索引 index 处插入元素 num
|
||||
/// </summary>
|
||||
public static void Insert(int[] nums, int num, int index)
|
||||
{
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
|
@ -41,7 +47,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
nums[index] = num;
|
||||
}
|
||||
|
||||
//删除索引 index 处元素
|
||||
/// <summary>
|
||||
/// 删除索引 index 处元素
|
||||
/// </summary>
|
||||
public static void Remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
|
@ -51,7 +59,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
}
|
||||
}
|
||||
|
||||
//遍历数组
|
||||
/// <summary>
|
||||
/// 遍历数组
|
||||
/// </summary>
|
||||
public static void Traverse(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
|
@ -67,7 +77,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
}
|
||||
}
|
||||
|
||||
//在数组中查找指定元素
|
||||
/// <summary>
|
||||
/// 在数组中查找指定元素
|
||||
/// </summary>
|
||||
public static int Find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
|
@ -78,7 +90,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
return -1;
|
||||
}
|
||||
|
||||
//辅助函数,数组转字符串
|
||||
/// <summary>
|
||||
/// 辅助函数,数组转字符串
|
||||
/// </summary>
|
||||
public static string ToString(int[] nums)
|
||||
{
|
||||
return string.Join(",", nums);
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
public class LinkedList
|
||||
{
|
||||
/// <summary>
|
||||
///在链表的结点 n0 之后插入结点 P
|
||||
/// 在链表的结点 n0 之后插入结点 P
|
||||
/// </summary>
|
||||
public static void Insert(ListNode n0, ListNode P)
|
||||
{
|
||||
|
@ -32,7 +32,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
///访问链表中索引为 index 的结点
|
||||
/// 访问链表中索引为 index 的结点
|
||||
/// </summary>
|
||||
public static ListNode Access(ListNode head, int index)
|
||||
{
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
using NUnit.Framework;
|
||||
using Array = hello_algo.chapter_array_and_linkedlist.Array;
|
||||
|
||||
namespace hello_algo.Test.chapter_array_and_linkedlist
|
||||
namespace hello_algo.test.chapter_array_and_linkedlist
|
||||
{
|
||||
[TestFixture]
|
||||
internal class ArrayTest
|
||||
|
@ -15,14 +15,14 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[SetUp]
|
||||
public void setup()
|
||||
{
|
||||
//初始化数组
|
||||
// 初始化数组
|
||||
nums = new int[] { 1, 3, 2, 5, 4 };
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestRandomAccess()
|
||||
{
|
||||
//随机访问
|
||||
// 随机访问
|
||||
int randomNum = Array.RandomAccess(nums);
|
||||
Console.WriteLine($"在 nums 中获取随机元素 {randomNum}");
|
||||
Assert.Contains(randomNum, nums);
|
||||
|
@ -31,7 +31,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestExtend()
|
||||
{
|
||||
//长度扩展
|
||||
// 长度扩展
|
||||
int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 };
|
||||
nums = Array.Extend(nums, 3);
|
||||
Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}");
|
||||
|
@ -41,7 +41,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestInsert()
|
||||
{
|
||||
//插入元素
|
||||
// 插入元素
|
||||
int[] target = { 1, 3, 2, 6, 5 };
|
||||
Array.Insert(nums, 6, 3);
|
||||
Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}");
|
||||
|
@ -51,7 +51,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestRemove()
|
||||
{
|
||||
//删除元素
|
||||
// 删除元素
|
||||
int[] target = { 1, 3, 5, 4, 4 };
|
||||
Array.Remove(nums, 2);
|
||||
Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}");
|
||||
|
@ -61,7 +61,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestFind()
|
||||
{
|
||||
//查找元素
|
||||
// 查找元素
|
||||
int index = Array.Find(nums, 3);
|
||||
Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index);
|
||||
Assert.AreEqual(1, index);
|
||||
|
|
|
@ -6,7 +6,7 @@ using hello_algo.chapter_array_and_linkedlist;
|
|||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.Test.chapter_array_and_linkedlist
|
||||
namespace hello_algo.test.chapter_array_and_linkedlist
|
||||
{
|
||||
[TestFixture]
|
||||
internal class LinkedListTest
|
||||
|
@ -36,7 +36,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void CheckInit()
|
||||
{
|
||||
//检查初始化是否正确
|
||||
// 检查初始化是否正确
|
||||
Console.WriteLine($"初始化的链表为{n0}");
|
||||
Assert.AreEqual(n0.ToString(), "1->3->2->5->4");
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestInsert()
|
||||
{
|
||||
//插入结点
|
||||
// 插入结点
|
||||
LinkedList.Insert(n0, new ListNode(0));
|
||||
Console.WriteLine($"插入结点后的链表为{n0}");
|
||||
Assert.AreEqual(n0.ToString(), "1->0->3->2->5->4");
|
||||
|
@ -53,7 +53,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestRemove()
|
||||
{
|
||||
//删除结点
|
||||
// 删除结点
|
||||
LinkedList.Remove(n0);
|
||||
Console.WriteLine($"删除节点后的链表为{n0}");
|
||||
Assert.AreEqual(n0.ToString(), "1->2->5->4");
|
||||
|
@ -62,7 +62,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestAccess()
|
||||
{
|
||||
//访问结点
|
||||
// 访问结点
|
||||
var node = LinkedList.Access(n0, 3);
|
||||
Console.WriteLine($"链表中索引 3 处的结点的值 ={node.val}");
|
||||
Assert.AreEqual(node.val, 5);
|
||||
|
@ -71,7 +71,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|||
[Test]
|
||||
public void TestFind()
|
||||
{
|
||||
//查找结点
|
||||
// 查找结点
|
||||
int index = LinkedList.Find(n0, 2);
|
||||
Console.WriteLine($"链表中值为 2 的结点的索引 = {index}");
|
||||
Assert.AreEqual(index, 2);
|
||||
|
|
Loading…
Reference in a new issue