mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Merge branch 'develop'
This commit is contained in:
		@ -47,13 +47,13 @@ int capacity(myList *list) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 访问元素 */
 | 
					/* 访问元素 */
 | 
				
			||||||
int get(myList *list, int index) {
 | 
					int get(myList *list, int index) {
 | 
				
			||||||
    assert(index < list->size);
 | 
					    assert(index >= 0 && index < list->size);
 | 
				
			||||||
    return list->nums[index];
 | 
					    return list->nums[index];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 更新元素 */
 | 
					/* 更新元素 */
 | 
				
			||||||
void set(myList *list, int index, int num) {
 | 
					void set(myList *list, int index, int num) {
 | 
				
			||||||
    assert(index < list->size);
 | 
					    assert(index >= 0 && index < list->size);
 | 
				
			||||||
    list->nums[index] = num;
 | 
					    list->nums[index] = num;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -68,7 +68,7 @@ void add(myList *list, int num) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 中间插入元素 */
 | 
					/* 中间插入元素 */
 | 
				
			||||||
void insert(myList *list, int index, int num) {
 | 
					void insert(myList *list, int index, int num) {
 | 
				
			||||||
    assert(index < size(list));
 | 
					    assert(index >= 0 && index < size(list));
 | 
				
			||||||
    for (int i = size(list); i > index; --i) {
 | 
					    for (int i = size(list); i > index; --i) {
 | 
				
			||||||
        list->nums[i] = list->nums[i - 1];
 | 
					        list->nums[i] = list->nums[i - 1];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -80,7 +80,7 @@ void insert(myList *list, int index, int num) {
 | 
				
			|||||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
 | 
					// 由于引入了 stdio.h ,此处无法使用 remove 关键词
 | 
				
			||||||
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
 | 
					// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
 | 
				
			||||||
int removeNum(myList *list, int index) {
 | 
					int removeNum(myList *list, int index) {
 | 
				
			||||||
    assert(index < size(list));
 | 
					    assert(index >= 0 && index < size(list));
 | 
				
			||||||
    int num = list->nums[index];
 | 
					    int num = list->nums[index];
 | 
				
			||||||
    for (int i = index; i < size(list) - 1; i++) {
 | 
					    for (int i = index; i < size(list) - 1; i++) {
 | 
				
			||||||
        list->nums[i] = list->nums[i + 1];
 | 
					        list->nums[i] = list->nums[i + 1];
 | 
				
			||||||
 | 
				
			|||||||
@ -38,14 +38,14 @@ public:
 | 
				
			|||||||
    /* 访问元素 */
 | 
					    /* 访问元素 */
 | 
				
			||||||
    int get(int index) {
 | 
					    int get(int index) {
 | 
				
			||||||
        // 索引如果越界则抛出异常,下同
 | 
					        // 索引如果越界则抛出异常,下同
 | 
				
			||||||
        if (index >= size())
 | 
					        if (index < 0 || index >= size())
 | 
				
			||||||
            throw out_of_range("索引越界");
 | 
					            throw out_of_range("索引越界");
 | 
				
			||||||
        return nums[index];
 | 
					        return nums[index];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 更新元素 */
 | 
					    /* 更新元素 */
 | 
				
			||||||
    void set(int index, int num) {
 | 
					    void set(int index, int num) {
 | 
				
			||||||
        if (index >= size())
 | 
					        if (index < 0 || index >= size())
 | 
				
			||||||
            throw out_of_range("索引越界");
 | 
					            throw out_of_range("索引越界");
 | 
				
			||||||
        nums[index] = num;
 | 
					        nums[index] = num;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -62,7 +62,7 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 中间插入元素 */
 | 
					    /* 中间插入元素 */
 | 
				
			||||||
    void insert(int index, int num) {
 | 
					    void insert(int index, int num) {
 | 
				
			||||||
        if (index >= size())
 | 
					        if (index < 0 || index >= size())
 | 
				
			||||||
            throw out_of_range("索引越界");
 | 
					            throw out_of_range("索引越界");
 | 
				
			||||||
        // 元素数量超出容量时,触发扩容机制
 | 
					        // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
        if (size() == capacity())
 | 
					        if (size() == capacity())
 | 
				
			||||||
@ -78,7 +78,7 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    int remove(int index) {
 | 
					    int remove(int index) {
 | 
				
			||||||
        if (index >= size())
 | 
					        if (index < 0 || index >= size())
 | 
				
			||||||
            throw out_of_range("索引越界");
 | 
					            throw out_of_range("索引越界");
 | 
				
			||||||
        int num = nums[index];
 | 
					        int num = nums[index];
 | 
				
			||||||
        // 索引 i 之后的元素都向前移动一位
 | 
					        // 索引 i 之后的元素都向前移动一位
 | 
				
			||||||
 | 
				
			|||||||
@ -37,7 +37,7 @@ namespace hello_algo.chapter_array_and_linkedlist
 | 
				
			|||||||
        public int Get(int index)
 | 
					        public int Get(int index)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            return nums[index];
 | 
					            return nums[index];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -45,7 +45,7 @@ namespace hello_algo.chapter_array_and_linkedlist
 | 
				
			|||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        public void Set(int index, int num)
 | 
					        public void Set(int index, int num)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            nums[index] = num;
 | 
					            nums[index] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -64,7 +64,7 @@ namespace hello_algo.chapter_array_and_linkedlist
 | 
				
			|||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        public void Insert(int index, int num)
 | 
					        public void Insert(int index, int num)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (size == Capacity())
 | 
					            if (size == Capacity())
 | 
				
			||||||
@ -82,7 +82,7 @@ namespace hello_algo.chapter_array_and_linkedlist
 | 
				
			|||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        public int Remove(int index)
 | 
					        public int Remove(int index)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            int num = nums[index];
 | 
					            int num = nums[index];
 | 
				
			||||||
            // 将索引 index 之后的元素都向前移动一位
 | 
					            // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
 | 
				
			|||||||
@ -35,7 +35,7 @@ func (l *myList) capacity() int {
 | 
				
			|||||||
/* 访问元素 */
 | 
					/* 访问元素 */
 | 
				
			||||||
func (l *myList) get(index int) int {
 | 
					func (l *myList) get(index int) int {
 | 
				
			||||||
	// 索引如果越界则抛出异常,下同
 | 
						// 索引如果越界则抛出异常,下同
 | 
				
			||||||
	if index >= l.numsSize {
 | 
						if index < 0 || index >= l.numsSize {
 | 
				
			||||||
		panic("索引越界")
 | 
							panic("索引越界")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return l.nums[index]
 | 
						return l.nums[index]
 | 
				
			||||||
@ -43,7 +43,7 @@ func (l *myList) get(index int) int {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 更新元素 */
 | 
					/* 更新元素 */
 | 
				
			||||||
func (l *myList) set(num, index int) {
 | 
					func (l *myList) set(num, index int) {
 | 
				
			||||||
	if index >= l.numsSize {
 | 
						if index < 0 || index >= l.numsSize {
 | 
				
			||||||
		panic("索引越界")
 | 
							panic("索引越界")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	l.nums[index] = num
 | 
						l.nums[index] = num
 | 
				
			||||||
@ -62,7 +62,7 @@ func (l *myList) add(num int) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 中间插入元素 */
 | 
					/* 中间插入元素 */
 | 
				
			||||||
func (l *myList) insert(num, index int) {
 | 
					func (l *myList) insert(num, index int) {
 | 
				
			||||||
	if index >= l.numsSize {
 | 
						if index < 0 || index >= l.numsSize {
 | 
				
			||||||
		panic("索引越界")
 | 
							panic("索引越界")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// 元素数量超出容量时,触发扩容机制
 | 
						// 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
@ -80,7 +80,7 @@ func (l *myList) insert(num, index int) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 删除元素 */
 | 
					/* 删除元素 */
 | 
				
			||||||
func (l *myList) remove(index int) int {
 | 
					func (l *myList) remove(index int) int {
 | 
				
			||||||
	if index >= l.numsSize {
 | 
						if index < 0 || index >= l.numsSize {
 | 
				
			||||||
		panic("索引越界")
 | 
							panic("索引越界")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	num := l.nums[index]
 | 
						num := l.nums[index]
 | 
				
			||||||
 | 
				
			|||||||
@ -33,14 +33,14 @@ class MyList {
 | 
				
			|||||||
    /* 访问元素 */
 | 
					    /* 访问元素 */
 | 
				
			||||||
    public int get(int index) {
 | 
					    public int get(int index) {
 | 
				
			||||||
        // 索引如果越界则抛出异常,下同
 | 
					        // 索引如果越界则抛出异常,下同
 | 
				
			||||||
        if (index >= size)
 | 
					        if (index < 0 || index >= size)
 | 
				
			||||||
            throw new IndexOutOfBoundsException("索引越界");
 | 
					            throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
        return nums[index];
 | 
					        return nums[index];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 更新元素 */
 | 
					    /* 更新元素 */
 | 
				
			||||||
    public void set(int index, int num) {
 | 
					    public void set(int index, int num) {
 | 
				
			||||||
        if (index >= size)
 | 
					        if (index < 0 || index >= size)
 | 
				
			||||||
            throw new IndexOutOfBoundsException("索引越界");
 | 
					            throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
        nums[index] = num;
 | 
					        nums[index] = num;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -57,7 +57,7 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 中间插入元素 */
 | 
					    /* 中间插入元素 */
 | 
				
			||||||
    public void insert(int index, int num) {
 | 
					    public void insert(int index, int num) {
 | 
				
			||||||
        if (index >= size)
 | 
					        if (index < 0 || index >= size)
 | 
				
			||||||
            throw new IndexOutOfBoundsException("索引越界");
 | 
					            throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
        // 元素数量超出容量时,触发扩容机制
 | 
					        // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
        if (size == capacity())
 | 
					        if (size == capacity())
 | 
				
			||||||
@ -73,7 +73,7 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    public int remove(int index) {
 | 
					    public int remove(int index) {
 | 
				
			||||||
        if (index >= size)
 | 
					        if (index < 0 || index >= size)
 | 
				
			||||||
            throw new IndexOutOfBoundsException("索引越界");
 | 
					            throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
        int num = nums[index];
 | 
					        int num = nums[index];
 | 
				
			||||||
        // 将索引 index 之后的元素都向前移动一位
 | 
					        // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
 | 
				
			|||||||
@ -29,15 +29,15 @@ class MyList {
 | 
				
			|||||||
    /* 访问元素 */
 | 
					    /* 访问元素 */
 | 
				
			||||||
    get(index) {
 | 
					    get(index) {
 | 
				
			||||||
        // 索引如果越界则抛出异常,下同
 | 
					        // 索引如果越界则抛出异常,下同
 | 
				
			||||||
        if (index >= this.#size) {
 | 
					        if (index < 0 || index >= this.#size)
 | 
				
			||||||
            throw new Error('索引越界');
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return this.#nums[index];
 | 
					        return this.#nums[index];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 更新元素 */
 | 
					    /* 更新元素 */
 | 
				
			||||||
    set(index, num) {
 | 
					    set(index, num) {
 | 
				
			||||||
        if (index >= this._size) throw new Error('索引越界');
 | 
					        if (index < 0 || index >= this.#size)
 | 
				
			||||||
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        this.#nums[index] = num;
 | 
					        this.#nums[index] = num;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -54,9 +54,8 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 中间插入元素 */
 | 
					    /* 中间插入元素 */
 | 
				
			||||||
    insert(index, num) {
 | 
					    insert(index, num) {
 | 
				
			||||||
        if (index >= this.#size) {
 | 
					        if (index < 0 || index >= this.#size)
 | 
				
			||||||
            throw new Error('索引越界');
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // 元素数量超出容量时,触发扩容机制
 | 
					        // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
        if (this.#size === this.#capacity) {
 | 
					        if (this.#size === this.#capacity) {
 | 
				
			||||||
            this.extendCapacity();
 | 
					            this.extendCapacity();
 | 
				
			||||||
@ -72,7 +71,8 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    remove(index) {
 | 
					    remove(index) {
 | 
				
			||||||
        if (index >= this.#size) throw new Error('索引越界');
 | 
					        if (index < 0 || index >= this.#size)
 | 
				
			||||||
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        let num = this.#nums[index];
 | 
					        let num = this.#nums[index];
 | 
				
			||||||
        // 将索引 index 之后的元素都向前移动一位
 | 
					        // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
        for (let j = index; j < this.#size - 1; j++) {
 | 
					        for (let j = index; j < this.#size - 1; j++) {
 | 
				
			||||||
 | 
				
			|||||||
@ -28,17 +28,17 @@ class MyList:
 | 
				
			|||||||
    """ 访问元素 """
 | 
					    """ 访问元素 """
 | 
				
			||||||
    def get(self, index):
 | 
					    def get(self, index):
 | 
				
			||||||
        # 索引如果越界则抛出异常,下同
 | 
					        # 索引如果越界则抛出异常,下同
 | 
				
			||||||
        assert index < self.__size, "索引越界"
 | 
					        assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
        return self.__nums[index]
 | 
					        return self.__nums[index]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    """ 更新元素 """
 | 
					    """ 更新元素 """
 | 
				
			||||||
    def set(self, num, index):
 | 
					    def set(self, num, index):
 | 
				
			||||||
        assert index < self.__size, "索引越界"
 | 
					        assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
        self.__nums[index] = num
 | 
					        self.__nums[index] = num
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    """ 中间插入(尾部添加)元素 """
 | 
					    """ 中间插入(尾部添加)元素 """
 | 
				
			||||||
    def add(self, num, index=-1):
 | 
					    def add(self, num, index=-1):
 | 
				
			||||||
        assert index < self.__size, "索引越界"
 | 
					        assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
        # 若不指定索引 index ,则向数组尾部添加元素
 | 
					        # 若不指定索引 index ,则向数组尾部添加元素
 | 
				
			||||||
        if index == -1:
 | 
					        if index == -1:
 | 
				
			||||||
            index = self.__size
 | 
					            index = self.__size
 | 
				
			||||||
@ -54,7 +54,7 @@ class MyList:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    """ 删除元素 """
 | 
					    """ 删除元素 """
 | 
				
			||||||
    def remove(self, index):
 | 
					    def remove(self, index):
 | 
				
			||||||
        assert index < self.__size, "索引越界"
 | 
					        assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
        num = self.nums[index]
 | 
					        num = self.nums[index]
 | 
				
			||||||
        # 索引 i 之后的元素都向前移动一位
 | 
					        # 索引 i 之后的元素都向前移动一位
 | 
				
			||||||
        for j in range(index, self.__size - 1):
 | 
					        for j in range(index, self.__size - 1):
 | 
				
			||||||
 | 
				
			|||||||
@ -29,7 +29,7 @@ class MyList {
 | 
				
			|||||||
    /* 访问元素 */
 | 
					    /* 访问元素 */
 | 
				
			||||||
    func get(index: Int) -> Int {
 | 
					    func get(index: Int) -> Int {
 | 
				
			||||||
        // 索引如果越界则抛出错误,下同
 | 
					        // 索引如果越界则抛出错误,下同
 | 
				
			||||||
        if index >= _size {
 | 
					        if index < 0 || index >= _size {
 | 
				
			||||||
            fatalError("索引越界")
 | 
					            fatalError("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return nums[index]
 | 
					        return nums[index]
 | 
				
			||||||
@ -37,7 +37,7 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 更新元素 */
 | 
					    /* 更新元素 */
 | 
				
			||||||
    func set(index: Int, num: Int) {
 | 
					    func set(index: Int, num: Int) {
 | 
				
			||||||
        if index >= _size {
 | 
					        if index < 0 || index >= _size {
 | 
				
			||||||
            fatalError("索引越界")
 | 
					            fatalError("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        nums[index] = num
 | 
					        nums[index] = num
 | 
				
			||||||
@ -56,7 +56,7 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 中间插入元素 */
 | 
					    /* 中间插入元素 */
 | 
				
			||||||
    func insert(index: Int, num: Int) {
 | 
					    func insert(index: Int, num: Int) {
 | 
				
			||||||
        if index >= _size {
 | 
					        if index < 0 || index >= _size {
 | 
				
			||||||
            fatalError("索引越界")
 | 
					            fatalError("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 元素数量超出容量时,触发扩容机制
 | 
					        // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
@ -75,7 +75,7 @@ class MyList {
 | 
				
			|||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    @discardableResult
 | 
					    @discardableResult
 | 
				
			||||||
    func remove(index: Int) -> Int {
 | 
					    func remove(index: Int) -> Int {
 | 
				
			||||||
        if index >= _size {
 | 
					        if index < 0 || index >= _size {
 | 
				
			||||||
            fatalError("索引越界")
 | 
					            fatalError("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        let num = nums[index]
 | 
					        let num = nums[index]
 | 
				
			||||||
 | 
				
			|||||||
@ -29,24 +29,23 @@ class MyList {
 | 
				
			|||||||
    /* 访问元素 */
 | 
					    /* 访问元素 */
 | 
				
			||||||
    public get(index: number): number {
 | 
					    public get(index: number): number {
 | 
				
			||||||
        // 索引如果越界则抛出异常,下同
 | 
					        // 索引如果越界则抛出异常,下同
 | 
				
			||||||
        if (index >= this._size) {
 | 
					        if (index < 0 || index >= this._size)
 | 
				
			||||||
            throw new Error('索引越界');
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return this.nums[index];
 | 
					        return this.nums[index];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 更新元素 */
 | 
					    /* 更新元素 */
 | 
				
			||||||
    public set(index: number, num: number): void {
 | 
					    public set(index: number, num: number): void {
 | 
				
			||||||
        if (index >= this._size) throw new Error('索引越界');
 | 
					        if (index < 0 || index >= this._size)
 | 
				
			||||||
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        this.nums[index] = num;
 | 
					        this.nums[index] = num;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 尾部添加元素 */
 | 
					    /* 尾部添加元素 */
 | 
				
			||||||
    public add(num: number): void {
 | 
					    public add(num: number): void {
 | 
				
			||||||
        // 如果长度等于容量,则需要扩容
 | 
					        // 如果长度等于容量,则需要扩容
 | 
				
			||||||
        if (this._size === this._capacity) {
 | 
					        if (this._size === this._capacity)
 | 
				
			||||||
            this.extendCapacity();
 | 
					            this.extendCapacity();
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // 将新元素添加到列表尾部
 | 
					        // 将新元素添加到列表尾部
 | 
				
			||||||
        this.nums[this._size] = num;
 | 
					        this.nums[this._size] = num;
 | 
				
			||||||
        this._size++;
 | 
					        this._size++;
 | 
				
			||||||
@ -54,9 +53,8 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 中间插入元素 */
 | 
					    /* 中间插入元素 */
 | 
				
			||||||
    public insert(index: number, num: number): void {
 | 
					    public insert(index: number, num: number): void {
 | 
				
			||||||
        if (index >= this._size) {
 | 
					        if (index < 0 || index >= this._size)
 | 
				
			||||||
            throw new Error('索引越界');
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // 元素数量超出容量时,触发扩容机制
 | 
					        // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
        if (this._size === this._capacity) {
 | 
					        if (this._size === this._capacity) {
 | 
				
			||||||
            this.extendCapacity();
 | 
					            this.extendCapacity();
 | 
				
			||||||
@ -72,7 +70,8 @@ class MyList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    public remove(index: number): number {
 | 
					    public remove(index: number): number {
 | 
				
			||||||
        if (index >= this._size) throw new Error('索引越界');
 | 
					        if (index < 0 || index >= this._size)
 | 
				
			||||||
 | 
					            throw new Error('索引越界');
 | 
				
			||||||
        let num = this.nums[index];
 | 
					        let num = this.nums[index];
 | 
				
			||||||
        // 将索引 index 之后的元素都向前移动一位
 | 
					        // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
        for (let j = index; j < this._size - 1; j++) {
 | 
					        for (let j = index; j < this._size - 1; j++) {
 | 
				
			||||||
 | 
				
			|||||||
@ -11,12 +11,12 @@ pub fn MyList(comptime T: type) type {
 | 
				
			|||||||
    return struct {
 | 
					    return struct {
 | 
				
			||||||
        const Self = @This();
 | 
					        const Self = @This();
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        nums: []T = undefined,                          // 数组(存储列表元素)
 | 
					        nums: []T = undefined,                        // 数组(存储列表元素)
 | 
				
			||||||
        numsCapacity: usize = 10,                       // 列表容量
 | 
					        numsCapacity: usize = 10,                     // 列表容量
 | 
				
			||||||
        numSize: usize = 0,                             // 列表长度(即当前元素数量)
 | 
					        numSize: usize = 0,                           // 列表长度(即当前元素数量)
 | 
				
			||||||
        extendRatio: usize = 2,                         // 每次列表扩容的倍数
 | 
					        extendRatio: usize = 2,                       // 每次列表扩容的倍数
 | 
				
			||||||
        mem_arena: ?std.heap.ArenaAllocator = null,
 | 
					        mem_arena: ?std.heap.ArenaAllocator = null,
 | 
				
			||||||
        mem_allocator: std.mem.Allocator = undefined,   // 内存分配器
 | 
					        mem_allocator: std.mem.Allocator = undefined, // 内存分配器
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 构造函数(分配内存+初始化列表)
 | 
					        // 构造函数(分配内存+初始化列表)
 | 
				
			||||||
        pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
 | 
					        pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
 | 
				
			||||||
@ -47,14 +47,14 @@ pub fn MyList(comptime T: type) type {
 | 
				
			|||||||
        // 访问元素
 | 
					        // 访问元素
 | 
				
			||||||
        pub fn get(self: *Self, index: usize) T {
 | 
					        pub fn get(self: *Self, index: usize) T {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= self.size()) @panic("索引越界");
 | 
					            if (index < 0 || index >= self.size()) @panic("索引越界");
 | 
				
			||||||
            return self.nums[index];
 | 
					            return self.nums[index];
 | 
				
			||||||
        }  
 | 
					        }  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 更新元素
 | 
					        // 更新元素
 | 
				
			||||||
        pub fn set(self: *Self, index: usize, num: T) void {
 | 
					        pub fn set(self: *Self, index: usize, num: T) void {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= self.size()) @panic("索引越界");
 | 
					            if (index < 0 || index >= self.size()) @panic("索引越界");
 | 
				
			||||||
            self.nums[index] = num;
 | 
					            self.nums[index] = num;
 | 
				
			||||||
        }  
 | 
					        }  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -69,7 +69,7 @@ pub fn MyList(comptime T: type) type {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // 中间插入元素
 | 
					        // 中间插入元素
 | 
				
			||||||
        pub fn insert(self: *Self, index: usize, num: T) !void {
 | 
					        pub fn insert(self: *Self, index: usize, num: T) !void {
 | 
				
			||||||
            if (index >= self.size()) @panic("索引越界");
 | 
					            if (index < 0 || index >= self.size()) @panic("索引越界");
 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (self.size() == self.capacity()) try self.extendCapacity();
 | 
					            if (self.size() == self.capacity()) try self.extendCapacity();
 | 
				
			||||||
            // 索引 i 以及之后的元素都向后移动一位
 | 
					            // 索引 i 以及之后的元素都向后移动一位
 | 
				
			||||||
@ -84,7 +84,7 @@ pub fn MyList(comptime T: type) type {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // 删除元素
 | 
					        // 删除元素
 | 
				
			||||||
        pub fn remove(self: *Self, index: usize) T {
 | 
					        pub fn remove(self: *Self, index: usize) T {
 | 
				
			||||||
            if (index >= self.size()) @panic("索引越界");
 | 
					            if (index < 0 || index >= self.size()) @panic("索引越界");
 | 
				
			||||||
            var num = self.nums[index];
 | 
					            var num = self.nums[index];
 | 
				
			||||||
            // 索引 i 之后的元素都向前移动一位
 | 
					            // 索引 i 之后的元素都向前移动一位
 | 
				
			||||||
            var j = index;
 | 
					            var j = index;
 | 
				
			||||||
 | 
				
			|||||||
@ -668,14 +668,14 @@ comments: true
 | 
				
			|||||||
        /* 访问元素 */
 | 
					        /* 访问元素 */
 | 
				
			||||||
        public int get(int index) {
 | 
					        public int get(int index) {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfBoundsException("索引越界");
 | 
					                throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
            return nums[index];
 | 
					            return nums[index];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        public void set(int index, int num) {
 | 
					        public void set(int index, int num) {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfBoundsException("索引越界");
 | 
					                throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
            nums[index] = num;
 | 
					            nums[index] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -692,7 +692,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        public void insert(int index, int num) {
 | 
					        public void insert(int index, int num) {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfBoundsException("索引越界");
 | 
					                throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (size == capacity())
 | 
					            if (size == capacity())
 | 
				
			||||||
@ -708,7 +708,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        public int remove(int index) {
 | 
					        public int remove(int index) {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfBoundsException("索引越界");
 | 
					                throw new IndexOutOfBoundsException("索引越界");
 | 
				
			||||||
            int num = nums[index];
 | 
					            int num = nums[index];
 | 
				
			||||||
            // 将索引 index 之后的元素都向前移动一位
 | 
					            // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
@ -766,14 +766,14 @@ comments: true
 | 
				
			|||||||
        /* 访问元素 */
 | 
					        /* 访问元素 */
 | 
				
			||||||
        int get(int index) {
 | 
					        int get(int index) {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= size())
 | 
					            if (index < 0 || index >= size())
 | 
				
			||||||
                throw out_of_range("索引越界");
 | 
					                throw out_of_range("索引越界");
 | 
				
			||||||
            return nums[index];
 | 
					            return nums[index];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        void set(int index, int num) {
 | 
					        void set(int index, int num) {
 | 
				
			||||||
            if (index >= size())
 | 
					            if (index < 0 || index >= size())
 | 
				
			||||||
                throw out_of_range("索引越界");
 | 
					                throw out_of_range("索引越界");
 | 
				
			||||||
            nums[index] = num;
 | 
					            nums[index] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -790,7 +790,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        void insert(int index, int num) {
 | 
					        void insert(int index, int num) {
 | 
				
			||||||
            if (index >= size())
 | 
					            if (index < 0 || index >= size())
 | 
				
			||||||
                throw out_of_range("索引越界");
 | 
					                throw out_of_range("索引越界");
 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (size() == capacity())
 | 
					            if (size() == capacity())
 | 
				
			||||||
@ -806,7 +806,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        int remove(int index) {
 | 
					        int remove(int index) {
 | 
				
			||||||
            if (index >= size())
 | 
					            if (index < 0 || index >= size())
 | 
				
			||||||
                throw out_of_range("索引越界");
 | 
					                throw out_of_range("索引越界");
 | 
				
			||||||
            int num = nums[index];
 | 
					            int num = nums[index];
 | 
				
			||||||
            // 索引 i 之后的元素都向前移动一位
 | 
					            // 索引 i 之后的元素都向前移动一位
 | 
				
			||||||
@ -859,17 +859,17 @@ comments: true
 | 
				
			|||||||
        """ 访问元素 """
 | 
					        """ 访问元素 """
 | 
				
			||||||
        def get(self, index):
 | 
					        def get(self, index):
 | 
				
			||||||
            # 索引如果越界则抛出异常,下同
 | 
					            # 索引如果越界则抛出异常,下同
 | 
				
			||||||
            assert index < self.__size, "索引越界"
 | 
					            assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
            return self.__nums[index]
 | 
					            return self.__nums[index]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        """ 更新元素 """
 | 
					        """ 更新元素 """
 | 
				
			||||||
        def set(self, num, index):
 | 
					        def set(self, num, index):
 | 
				
			||||||
            assert index < self.__size, "索引越界"
 | 
					            assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
            self.__nums[index] = num
 | 
					            self.__nums[index] = num
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        """ 中间插入(尾部添加)元素 """
 | 
					        """ 中间插入(尾部添加)元素 """
 | 
				
			||||||
        def add(self, num, index=-1):
 | 
					        def add(self, num, index=-1):
 | 
				
			||||||
            assert index < self.__size, "索引越界"
 | 
					            assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
            # 若不指定索引 index ,则向数组尾部添加元素
 | 
					            # 若不指定索引 index ,则向数组尾部添加元素
 | 
				
			||||||
            if index == -1:
 | 
					            if index == -1:
 | 
				
			||||||
                index = self.__size
 | 
					                index = self.__size
 | 
				
			||||||
@ -885,12 +885,15 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        """ 删除元素 """
 | 
					        """ 删除元素 """
 | 
				
			||||||
        def remove(self, index):
 | 
					        def remove(self, index):
 | 
				
			||||||
            assert index < self.__size, "索引越界"
 | 
					            assert index >= 0 and index < self.__size, "索引越界"
 | 
				
			||||||
 | 
					            num = self.nums[index]
 | 
				
			||||||
            # 索引 i 之后的元素都向前移动一位
 | 
					            # 索引 i 之后的元素都向前移动一位
 | 
				
			||||||
            for j in range(index, self.__size - 1):
 | 
					            for j in range(index, self.__size - 1):
 | 
				
			||||||
                self.__nums[j] = self.__nums[j + 1]
 | 
					                self.__nums[j] = self.__nums[j + 1]
 | 
				
			||||||
            # 更新元素数量
 | 
					            # 更新元素数量
 | 
				
			||||||
            self.__size -= 1
 | 
					            self.__size -= 1
 | 
				
			||||||
 | 
					            # 返回被删除元素
 | 
				
			||||||
 | 
					            return num
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        """ 列表扩容 """
 | 
					        """ 列表扩容 """
 | 
				
			||||||
        def extend_capacity(self):
 | 
					        def extend_capacity(self):
 | 
				
			||||||
@ -934,7 +937,7 @@ comments: true
 | 
				
			|||||||
    /* 访问元素 */
 | 
					    /* 访问元素 */
 | 
				
			||||||
    func (l *myList) get(index int) int {
 | 
					    func (l *myList) get(index int) int {
 | 
				
			||||||
        // 索引如果越界则抛出异常,下同
 | 
					        // 索引如果越界则抛出异常,下同
 | 
				
			||||||
        if index >= l.numsSize {
 | 
					        if index < 0 || index >= l.numsSize {
 | 
				
			||||||
            panic("索引越界")
 | 
					            panic("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return l.nums[index]
 | 
					        return l.nums[index]
 | 
				
			||||||
@ -942,7 +945,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 更新元素 */
 | 
					    /* 更新元素 */
 | 
				
			||||||
    func (l *myList) set(num, index int) {
 | 
					    func (l *myList) set(num, index int) {
 | 
				
			||||||
        if index >= l.numsSize {
 | 
					        if index < 0 || index >= l.numsSize {
 | 
				
			||||||
            panic("索引越界")
 | 
					            panic("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        l.nums[index] = num
 | 
					        l.nums[index] = num
 | 
				
			||||||
@ -961,7 +964,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 中间插入元素 */
 | 
					    /* 中间插入元素 */
 | 
				
			||||||
    func (l *myList) insert(num, index int) {
 | 
					    func (l *myList) insert(num, index int) {
 | 
				
			||||||
        if index >= l.numsSize {
 | 
					        if index < 0 || index >= l.numsSize {
 | 
				
			||||||
            panic("索引越界")
 | 
					            panic("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 元素数量超出容量时,触发扩容机制
 | 
					        // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
@ -979,7 +982,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    func (l *myList) remove(index int) int {
 | 
					    func (l *myList) remove(index int) int {
 | 
				
			||||||
        if index >= l.numsSize {
 | 
					        if index < 0 || index >= l.numsSize {
 | 
				
			||||||
            panic("索引越界")
 | 
					            panic("索引越界")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        num := l.nums[index]
 | 
					        num := l.nums[index]
 | 
				
			||||||
@ -1030,15 +1033,15 @@ comments: true
 | 
				
			|||||||
        /* 访问元素 */
 | 
					        /* 访问元素 */
 | 
				
			||||||
        get(index) {
 | 
					        get(index) {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= this.#size) {
 | 
					            if (index < 0 || index >= this.#size)
 | 
				
			||||||
                throw new Error('索引越界');
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            return this.#nums[index];
 | 
					            return this.#nums[index];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        set(index, num) {
 | 
					        set(index, num) {
 | 
				
			||||||
            if (index >= this._size) throw new Error('索引越界');
 | 
					            if (index < 0 || index >= this.#size)
 | 
				
			||||||
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            this.#nums[index] = num;
 | 
					            this.#nums[index] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1055,9 +1058,8 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        insert(index, num) {
 | 
					        insert(index, num) {
 | 
				
			||||||
            if (index >= this.#size) {
 | 
					            if (index < 0 || index >= this.#size)
 | 
				
			||||||
                throw new Error('索引越界');
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (this.#size === this.#capacity) {
 | 
					            if (this.#size === this.#capacity) {
 | 
				
			||||||
                this.extendCapacity();
 | 
					                this.extendCapacity();
 | 
				
			||||||
@ -1073,7 +1075,8 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        remove(index) {
 | 
					        remove(index) {
 | 
				
			||||||
            if (index >= this.#size) throw new Error('索引越界');
 | 
					            if (index < 0 || index >= this.#size)
 | 
				
			||||||
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            let num = this.#nums[index];
 | 
					            let num = this.#nums[index];
 | 
				
			||||||
            // 将索引 index 之后的元素都向前移动一位
 | 
					            // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
            for (let j = index; j < this.#size - 1; j++) {
 | 
					            for (let j = index; j < this.#size - 1; j++) {
 | 
				
			||||||
@ -1125,24 +1128,23 @@ comments: true
 | 
				
			|||||||
        /* 访问元素 */
 | 
					        /* 访问元素 */
 | 
				
			||||||
        public get(index: number): number {
 | 
					        public get(index: number): number {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= this._size) {
 | 
					            if (index < 0 || index >= this._size)
 | 
				
			||||||
                throw new Error('索引越界');
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            return this.nums[index];
 | 
					            return this.nums[index];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        public set(index: number, num: number): void {
 | 
					        public set(index: number, num: number): void {
 | 
				
			||||||
            if (index >= this._size) throw new Error('索引越界');
 | 
					            if (index < 0 || index >= this._size)
 | 
				
			||||||
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            this.nums[index] = num;
 | 
					            this.nums[index] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 尾部添加元素 */
 | 
					        /* 尾部添加元素 */
 | 
				
			||||||
        public add(num: number): void {
 | 
					        public add(num: number): void {
 | 
				
			||||||
            // 如果长度等于容量,则需要扩容
 | 
					            // 如果长度等于容量,则需要扩容
 | 
				
			||||||
            if (this._size === this._capacity) {
 | 
					            if (this._size === this._capacity)
 | 
				
			||||||
                this.extendCapacity();
 | 
					                this.extendCapacity();
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            // 将新元素添加到列表尾部
 | 
					            // 将新元素添加到列表尾部
 | 
				
			||||||
            this.nums[this._size] = num;
 | 
					            this.nums[this._size] = num;
 | 
				
			||||||
            this._size++;
 | 
					            this._size++;
 | 
				
			||||||
@ -1150,9 +1152,8 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        public insert(index: number, num: number): void {
 | 
					        public insert(index: number, num: number): void {
 | 
				
			||||||
            if (index >= this._size) {
 | 
					            if (index < 0 || index >= this._size)
 | 
				
			||||||
                throw new Error('索引越界');
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (this._size === this._capacity) {
 | 
					            if (this._size === this._capacity) {
 | 
				
			||||||
                this.extendCapacity();
 | 
					                this.extendCapacity();
 | 
				
			||||||
@ -1168,7 +1169,8 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        public remove(index: number): number {
 | 
					        public remove(index: number): number {
 | 
				
			||||||
            if (index >= this._size) throw new Error('索引越界');
 | 
					            if (index < 0 || index >= this._size)
 | 
				
			||||||
 | 
					                throw new Error('索引越界');
 | 
				
			||||||
            let num = this.nums[index];
 | 
					            let num = this.nums[index];
 | 
				
			||||||
            // 将索引 index 之后的元素都向前移动一位
 | 
					            // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
            for (let j = index; j < this._size - 1; j++) {
 | 
					            for (let j = index; j < this._size - 1; j++) {
 | 
				
			||||||
@ -1230,7 +1232,7 @@ comments: true
 | 
				
			|||||||
        public int Get(int index)
 | 
					        public int Get(int index)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            // 索引如果越界则抛出异常,下同
 | 
					            // 索引如果越界则抛出异常,下同
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            return nums[index];
 | 
					            return nums[index];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -1238,7 +1240,7 @@ comments: true
 | 
				
			|||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        public void Set(int index, int num)
 | 
					        public void Set(int index, int num)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            nums[index] = num;
 | 
					            nums[index] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -1257,7 +1259,7 @@ comments: true
 | 
				
			|||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        public void Insert(int index, int num)
 | 
					        public void Insert(int index, int num)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
            if (size == Capacity())
 | 
					            if (size == Capacity())
 | 
				
			||||||
@ -1275,7 +1277,7 @@ comments: true
 | 
				
			|||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        public int Remove(int index)
 | 
					        public int Remove(int index)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (index >= size)
 | 
					            if (index < 0 || index >= size)
 | 
				
			||||||
                throw new IndexOutOfRangeException("索引越界");
 | 
					                throw new IndexOutOfRangeException("索引越界");
 | 
				
			||||||
            int num = nums[index];
 | 
					            int num = nums[index];
 | 
				
			||||||
            // 将索引 index 之后的元素都向前移动一位
 | 
					            // 将索引 index 之后的元素都向前移动一位
 | 
				
			||||||
@ -1328,7 +1330,7 @@ comments: true
 | 
				
			|||||||
        /* 访问元素 */
 | 
					        /* 访问元素 */
 | 
				
			||||||
        func get(index: Int) -> Int {
 | 
					        func get(index: Int) -> Int {
 | 
				
			||||||
            // 索引如果越界则抛出错误,下同
 | 
					            // 索引如果越界则抛出错误,下同
 | 
				
			||||||
            if index >= _size {
 | 
					            if index < 0 || index >= _size {
 | 
				
			||||||
                fatalError("索引越界")
 | 
					                fatalError("索引越界")
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return nums[index]
 | 
					            return nums[index]
 | 
				
			||||||
@ -1336,7 +1338,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 更新元素 */
 | 
					        /* 更新元素 */
 | 
				
			||||||
        func set(index: Int, num: Int) {
 | 
					        func set(index: Int, num: Int) {
 | 
				
			||||||
            if index >= _size {
 | 
					            if index < 0 || index >= _size {
 | 
				
			||||||
                fatalError("索引越界")
 | 
					                fatalError("索引越界")
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            nums[index] = num
 | 
					            nums[index] = num
 | 
				
			||||||
@ -1355,7 +1357,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        /* 中间插入元素 */
 | 
					        /* 中间插入元素 */
 | 
				
			||||||
        func insert(index: Int, num: Int) {
 | 
					        func insert(index: Int, num: Int) {
 | 
				
			||||||
            if index >= _size {
 | 
					            if index < 0 || index >= _size {
 | 
				
			||||||
                fatalError("索引越界")
 | 
					                fatalError("索引越界")
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            // 元素数量超出容量时,触发扩容机制
 | 
					            // 元素数量超出容量时,触发扩容机制
 | 
				
			||||||
@ -1374,7 +1376,7 @@ comments: true
 | 
				
			|||||||
        /* 删除元素 */
 | 
					        /* 删除元素 */
 | 
				
			||||||
        @discardableResult
 | 
					        @discardableResult
 | 
				
			||||||
        func remove(index: Int) -> Int {
 | 
					        func remove(index: Int) -> Int {
 | 
				
			||||||
            if index >= _size {
 | 
					            if index < 0 || index >= _size {
 | 
				
			||||||
                fatalError("索引越界")
 | 
					                fatalError("索引越界")
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            let num = nums[index]
 | 
					            let num = nums[index]
 | 
				
			||||||
@ -1395,14 +1397,5 @@ comments: true
 | 
				
			|||||||
            // 更新列表容量
 | 
					            // 更新列表容量
 | 
				
			||||||
            _capacity = nums.count
 | 
					            _capacity = nums.count
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        /* 将列表转换为数组 */
 | 
					 | 
				
			||||||
        func toArray() -> [Int] {
 | 
					 | 
				
			||||||
            var nums = Array(repeating: 0, count: _size)
 | 
					 | 
				
			||||||
            for i in 0 ..< _size {
 | 
					 | 
				
			||||||
                nums[i] = get(index: i)
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            return nums
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user