mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 10:20:40 +08:00
build
This commit is contained in:
@@ -213,8 +213,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 随机返回一个数组元素 */
|
||||
int randomAccess(int[] nums)
|
||||
{
|
||||
int randomAccess(int[] nums) {
|
||||
Random random = new();
|
||||
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
@@ -378,13 +377,11 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 扩展数组长度 */
|
||||
int[] extend(int[] nums, int enlarge)
|
||||
{
|
||||
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];
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
@@ -529,11 +526,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(int[] nums, int num, int index)
|
||||
{
|
||||
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 处元素
|
||||
@@ -648,11 +643,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 删除索引 index 处元素 */
|
||||
void remove(int[] nums, int index)
|
||||
{
|
||||
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];
|
||||
}
|
||||
}
|
||||
@@ -807,17 +800,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 遍历数组 */
|
||||
void traverse(int[] nums)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
@@ -957,10 +947,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 在数组中查找指定元素 */
|
||||
int find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
int find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -427,8 +427,7 @@ comments: true
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
void insert(ListNode n0, ListNode P)
|
||||
{
|
||||
void insert(ListNode n0, ListNode P) {
|
||||
ListNode? n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
@@ -570,8 +569,7 @@ comments: true
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
void remove(ListNode n0)
|
||||
{
|
||||
void remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
@@ -716,10 +714,8 @@ comments: true
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
ListNode? access(ListNode head, int index)
|
||||
{
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
ListNode? access(ListNode head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
@@ -882,11 +878,9 @@ comments: true
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
int find(ListNode head, int target)
|
||||
{
|
||||
int find(ListNode head, int target) {
|
||||
int index = 0;
|
||||
while (head != null)
|
||||
{
|
||||
while (head != null) {
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
|
||||
@@ -954,12 +954,14 @@ comments: true
|
||||
def get(self, index: int) -> int:
|
||||
"""访问元素"""
|
||||
# 索引如果越界则抛出异常,下同
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
return self.__nums[index]
|
||||
|
||||
def set(self, num: int, index: int) -> None:
|
||||
"""更新元素"""
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
self.__nums[index] = num
|
||||
|
||||
def add(self, num: int) -> None:
|
||||
@@ -972,7 +974,8 @@ comments: true
|
||||
|
||||
def insert(self, num: int, index: int) -> None:
|
||||
"""中间插入元素"""
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.__size == self.capacity():
|
||||
self.extend_capacity()
|
||||
@@ -985,7 +988,8 @@ comments: true
|
||||
|
||||
def remove(self, index: int) -> int:
|
||||
"""删除元素"""
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
num = self.__nums[index]
|
||||
# 索引 i 之后的元素都向前移动一位
|
||||
for j in range(index, self.__size - 1):
|
||||
@@ -1428,34 +1432,29 @@ comments: true
|
||||
|
||||
```csharp title="my_list.cs"
|
||||
/* 列表类简易实现 */
|
||||
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("索引越界");
|
||||
@@ -1463,16 +1462,14 @@ comments: true
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
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();
|
||||
@@ -1482,16 +1479,14 @@ comments: true
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
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;
|
||||
@@ -1500,14 +1495,12 @@ comments: true
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
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];
|
||||
}
|
||||
// 更新元素数量
|
||||
@@ -1517,8 +1510,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity()
|
||||
{
|
||||
public void extendCapacity() {
|
||||
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, numsCapacity * extendRatio);
|
||||
// 更新列表容量
|
||||
@@ -1526,12 +1518,10 @@ comments: true
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user