Skip to content

Commit 05fb591

Browse files
author
lijialin
committed
数组支持任意位置插入修改
1 parent 9c788bd commit 05fb591

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

python/05_array/myarray.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ def insert_v2(self, index: int, value: int) -> bool:
7373
# 插入位置大于当前的元素个数,可以插入最后的位置
7474
if index >= self._count:
7575
self._data.append(value)
76+
elif index < 0:
77+
# 位置小于 0 可以插入第 0 个位置
78+
self._data.insert(0, value)
7679
else:
77-
if index < 0:
78-
# 位置小于 0 可以插入第 0 个位置
79-
self._data.insert(0, value)
80-
else:
81-
# 挪动 index 至 _count 位到 index+1 至 _count+1 位
82-
# 插入第 index
83-
self._data[index+1:self._count+1] = self._data[index:self._count]
84-
self._data[index] = value
80+
# 挪动 index 至 _count 位到 index+1 至 _count+1 位
81+
# 插入第 index
82+
self._data[index+1:self._count+1] = self._data[index:self._count]
83+
self._data[index] = value
8584

8685
self._count += 1
8786
return True

0 commit comments

Comments
 (0)