mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-23 18:11:45 +08:00
build
This commit is contained in:
@@ -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