Fix the index out of bound check in my_list.

This commit is contained in:
Yudong Jin
2023-01-30 17:50:07 +08:00
parent 15c798046a
commit ddd5562b60
11 changed files with 90 additions and 98 deletions

View File

@ -29,7 +29,7 @@ class MyList {
/* 访 */
func get(index: Int) -> Int {
//
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
return nums[index]
@ -37,7 +37,7 @@ class MyList {
/* */
func set(index: Int, num: Int) {
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
nums[index] = num
@ -56,7 +56,7 @@ class MyList {
/* */
func insert(index: Int, num: Int) {
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
//
@ -75,7 +75,7 @@ class MyList {
/* */
@discardableResult
func remove(index: Int) -> Int {
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
let num = nums[index]