mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-29 00:54:26 +00:00
Reformat the C# codes.
Disable creating new line before open brace.
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user