|
|
@ -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);
|
|
|
|