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