commit
31823c795d
@ -9,35 +9,53 @@ class MyArray:
|
|||||||
"""A simple wrapper around List.
|
"""A simple wrapper around List.
|
||||||
You cannot have -1 in the array.
|
You cannot have -1 in the array.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, capacity: int):
|
def __init__(self, capacity: int):
|
||||||
|
|
||||||
self._data = []
|
self._data = []
|
||||||
self._count = 0
|
self._count = 0
|
||||||
self._capacity = capacity
|
self._capacity = capacity
|
||||||
|
|
||||||
def __getitem__(self, position: int) -> int:
|
def __getitem__(self, position: int) -> int:
|
||||||
|
|
||||||
"""Support for subscript.
|
"""Support for subscript.
|
||||||
Perhaps better than the find() method below.
|
Perhaps better than the find() method below.
|
||||||
"""
|
"""
|
||||||
return self._data[position]
|
return self._data[position]
|
||||||
|
|
||||||
def find(self, index: int) -> Optional[int]:
|
def find(self, index: int) -> Optional[int]:
|
||||||
|
|
||||||
if index >= self._count or index <= -self._count: return None
|
if index >= self._count or index <= -self._count: return None
|
||||||
return self._data[index]
|
return self._data[index]
|
||||||
|
|
||||||
def delete(self, index: int) -> bool:
|
def delete(self, index: int) -> bool:
|
||||||
|
|
||||||
if index >= self._count or index <= -self._count: return False
|
if index >= self._count or index <= -self._count: return False
|
||||||
|
|
||||||
self._data[index:-1] = self._data[index+1:]
|
self._data[index:-1] = self._data[index+1:]
|
||||||
self._count -= 1
|
self._count -= 1
|
||||||
|
# 真正将数据删除并覆盖原来的数据 ,这个需要增加
|
||||||
|
self._data = self._data[0:self._count]
|
||||||
|
print ('delet function',self._data)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def insert(self, index: int, value: int) -> bool:
|
def insert(self, index: int, value: int) -> bool:
|
||||||
if index >= self._count or index <= -self._count: return False
|
|
||||||
|
#if index >= self._count or index <= -self._count: return False
|
||||||
if self._capacity == self._count: return False
|
if self._capacity == self._count: return False
|
||||||
self._data.insert(index, value)
|
# 如果还有空间,那么插入位置大于当前的元素个数,可以插入最后的位置
|
||||||
|
if index >= self._count:
|
||||||
|
self._data.append(value)
|
||||||
|
# 同上,如果位置小于0 可以插入第0个位置.
|
||||||
|
if index < 0:
|
||||||
|
print (index)
|
||||||
|
self._data.insert(0, value)
|
||||||
|
|
||||||
self._count += 1
|
self._count += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def insert_to_tail(self, value: int) -> bool:
|
def insert_to_tail(self, value: int) -> bool:
|
||||||
|
|
||||||
if self._count == self._capacity: return False
|
if self._count == self._capacity: return False
|
||||||
if self._count == len(self._data):
|
if self._count == len(self._data):
|
||||||
self._data.append(value)
|
self._data.append(value)
|
||||||
@ -47,11 +65,13 @@ class MyArray:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
|
||||||
return " ".join(str(num) for num in self._data[:self._count])
|
return " ".join(str(num) for num in self._data[:self._count])
|
||||||
|
|
||||||
def print_all(self):
|
def print_all(self):
|
||||||
|
|
||||||
for num in self._data[:self._count]:
|
for num in self._data[:self._count]:
|
||||||
print(f"{num}", end=" ")
|
print("{num}", end=" ")
|
||||||
print("\n", flush=True)
|
print("\n", flush=True)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -59,7 +79,10 @@ if __name__ == "__main__":
|
|||||||
for i in range(6):
|
for i in range(6):
|
||||||
a.insert_to_tail(i)
|
a.insert_to_tail(i)
|
||||||
|
|
||||||
a.delete(2)
|
print('origin',a)
|
||||||
print(a)
|
a.delete(4)
|
||||||
a.insert_to_tail(7)
|
print ('delete ',a)
|
||||||
print(a)
|
|
||||||
|
a.insert(100,10000)
|
||||||
|
print (a)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user