Reformat the C# codes.

Disable creating new line before open brace.
This commit is contained in:
krahets
2023-04-23 03:03:12 +08:00
parent ac6eece4f3
commit 73dcb4cea9
49 changed files with 561 additions and 1135 deletions

View File

@ -6,11 +6,9 @@ using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist;
public class array
{
public class array {
/* 随机返回一个数组元素 */
public static int randomAccess(int[] nums)
{
public static int randomAccess(int[] nums) {
Random random = new();
// 在区间 [0, nums.Length) 中随机抽取一个数字
int randomIndex = random.Next(nums.Length);
@ -20,13 +18,11 @@ public class array
}
/* 扩展数组长度 */
public static int[] extend(int[] nums, int enlarge)
{
public static int[] extend(int[] nums, int enlarge) {
// 初始化一个扩展长度后的数组
int[] res = new int[nums.Length + enlarge];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < nums.Length; i++)
{
for (int i = 0; i < nums.Length; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
@ -34,11 +30,9 @@ public class array
}
/* 在数组的索引 index 处插入元素 num */
public static void insert(int[] nums, int num, int index)
{
public static void insert(int[] nums, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = nums.Length - 1; i > index; i--)
{
for (int i = nums.Length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
@ -46,36 +40,29 @@ public class array
}
/* 删除索引 index 处元素 */
public static void remove(int[] nums, int index)
{
public static void remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++)
{
for (int i = index; i < nums.Length - 1; i++) {
nums[i] = nums[i + 1];
}
}
/* 遍历数组 */
public static void traverse(int[] nums)
{
public static void traverse(int[] nums) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.Length; i++)
{
for (int i = 0; i < nums.Length; i++) {
count++;
}
// 直接遍历数组
foreach (int num in nums)
{
foreach (int num in nums) {
count++;
}
}
/* 在数组中查找指定元素 */
public static int find(int[] nums, int target)
{
for (int i = 0; i < nums.Length; i++)
{
public static int find(int[] nums, int target) {
for (int i = 0; i < nums.Length; i++) {
if (nums[i] == target)
return i;
}
@ -83,15 +70,13 @@ public class array
}
/* 辅助函数,数组转字符串 */
public static string toString(int[] nums)
{
public static string toString(int[] nums) {
return string.Join(",", nums);
}
[Test]
public static void Test()
{
public static void Test() {
// 初始化数组
int[] arr = new int[5];
Console.WriteLine("数组 arr = " + toString(arr));

View File

@ -7,19 +7,16 @@ using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist;
public class linked_list
{
public class linked_list {
/* 在链表的节点 n0 之后插入节点 P */
public static void insert(ListNode n0, ListNode P)
{
public static void insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* 删除链表的节点 n0 之后的首个节点 */
public static void remove(ListNode n0)
{
public static void remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
@ -29,10 +26,8 @@ public class linked_list
}
/* 访问链表中索引为 index 的节点 */
public static ListNode? access(ListNode head, int index)
{
for (int i = 0; i < index; i++)
{
public static ListNode? access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
head = head.next;
@ -41,11 +36,9 @@ public class linked_list
}
/* 在链表中查找值为 target 的首个节点 */
public static int find(ListNode head, int target)
{
public static int find(ListNode head, int target) {
int index = 0;
while (head != null)
{
while (head != null) {
if (head.val == target)
return index;
head = head.next;
@ -56,8 +49,7 @@ public class linked_list
[Test]
public void Test()
{
public void Test() {
// 初始化链表
// 初始化各个节点
ListNode n0 = new ListNode(1);

View File

@ -8,11 +8,9 @@ using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist;
public class list
{
public class list {
[Test]
public void Test()
{
public void Test() {
/* 初始化列表 */
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
@ -49,15 +47,13 @@ public class list
/* 通过索引遍历列表 */
int count = 0;
for (int i = 0; i < list.Count(); i++)
{
for (int i = 0; i < list.Count(); i++) {
count++;
}
/* 直接遍历列表元素 */
count = 0;
foreach (int n in list)
{
foreach (int n in list) {
count++;
}

View File

@ -9,34 +9,29 @@ using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist;
/* 列表类简易实现 */
class MyList
{
class MyList {
private int[] nums; // 数组(存储列表元素)
private int numsCapacity = 10; // 列表容量
private int numsSize = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
public MyList()
{
public MyList() {
nums = new int[numsCapacity];
}
/* 获取列表长度(即当前元素数量)*/
public int size()
{
public int size() {
return numsSize;
}
/* 获取列表容量 */
public int capacity()
{
public int capacity() {
return numsCapacity;
}
/* 访问元素 */
public int get(int index)
{
public int get(int index) {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
@ -44,16 +39,14 @@ class MyList
}
/* 更新元素 */
public void set(int index, int num)
{
public void set(int index, int num) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
public void add(int num)
{
public void add(int num) {
// 元素数量超出容量时,触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
@ -63,16 +56,14 @@ class MyList
}
/* 中间插入元素 */
public void insert(int index, int num)
{
public void insert(int index, int num) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
// 将索引 index 以及之后的元素都向后移动一位
for (int j = numsSize - 1; j >= index; j--)
{
for (int j = numsSize - 1; j >= index; j--) {
nums[j + 1] = nums[j];
}
nums[index] = num;
@ -81,14 +72,12 @@ class MyList
}
/* 删除元素 */
public int remove(int index)
{
public int remove(int index) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
// 将索引 index 之后的元素都向前移动一位
for (int j = index; j < numsSize - 1; j++)
{
for (int j = index; j < numsSize - 1; j++) {
nums[j] = nums[j + 1];
}
// 更新元素数量
@ -98,8 +87,7 @@ class MyList
}
/* 列表扩容 */
public void extendCapacity()
{
public void extendCapacity() {
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
System.Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量
@ -107,23 +95,19 @@ class MyList
}
/* 将列表转换为数组 */
public int[] toArray()
{
public int[] toArray() {
// 仅转换有效长度范围内的列表元素
int[] nums = new int[numsSize];
for (int i = 0; i < numsSize; i++)
{
for (int i = 0; i < numsSize; i++) {
nums[i] = get(i);
}
return nums;
}
}
public class my_list
{
public class my_list {
[Test]
public void Test()
{
public void Test() {
/* 初始化列表 */
MyList list = new MyList();
/* 尾部添加元素 */
@ -152,8 +136,7 @@ public class my_list
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.toArray()));
/* 测试扩容机制 */
for (int i = 0; i < 10; i++)
{
for (int i = 0; i < 10; i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
list.add(i);
}