mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 06:26:08 +00:00
build
This commit is contained in:
@@ -149,8 +149,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 随机访问元素 """
|
||||
def random_access(nums):
|
||||
""" 随机访问元素 """
|
||||
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
|
||||
random_index = random.randint(0, len(nums) - 1)
|
||||
# 获取并返回随机元素
|
||||
@@ -285,10 +285,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 扩展数组长度 """
|
||||
# 请注意,Python 的 list 是动态数组,可以直接扩展
|
||||
# 为了方便学习,本函数将 list 看作是长度不可变的数组
|
||||
def extend(nums, enlarge):
|
||||
""" 扩展数组长度 """
|
||||
# 初始化一个扩展长度后的数组
|
||||
res = [0] * (len(nums) + enlarge)
|
||||
# 将原数组中的所有元素复制到新数组
|
||||
@@ -442,8 +440,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 在数组的索引 index 处插入元素 num """
|
||||
def insert(nums, num, index):
|
||||
""" 在数组的索引 index 处插入元素 num """
|
||||
# 把索引 index 以及之后的所有元素向后移动一位
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
nums[i] = nums[i - 1]
|
||||
@@ -562,8 +560,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 删除索引 index 处元素 """
|
||||
def remove(nums, index):
|
||||
""" 删除索引 index 处元素 """
|
||||
# 把索引 index 之后的所有元素向前移动一位
|
||||
for i in range(index, len(nums) - 1):
|
||||
nums[i] = nums[i + 1]
|
||||
@@ -694,8 +692,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 遍历数组 """
|
||||
def traverse(nums):
|
||||
""" 遍历数组 """
|
||||
count = 0
|
||||
# 通过索引遍历数组
|
||||
for i in range(len(nums)):
|
||||
@@ -850,8 +848,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 在数组中查找指定元素 """
|
||||
def find(nums, target):
|
||||
""" 在数组中查找指定元素 """
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target:
|
||||
return i
|
||||
|
||||
@@ -351,8 +351,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 在链表的结点 n0 之后插入结点 P """
|
||||
def insert(n0, P):
|
||||
""" 在链表的结点 n0 之后插入结点 P """
|
||||
n1 = n0.next
|
||||
P.next = n1
|
||||
n0.next = P
|
||||
@@ -470,8 +470,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 删除链表的结点 n0 之后的首个结点 """
|
||||
def remove(n0):
|
||||
""" 删除链表的结点 n0 之后的首个结点 """
|
||||
if not n0.next:
|
||||
return
|
||||
# n0 -> P -> n1
|
||||
@@ -609,8 +609,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 访问链表中索引为 index 的结点 """
|
||||
def access(head, index):
|
||||
""" 访问链表中索引为 index 的结点 """
|
||||
for _ in range(index):
|
||||
if not head:
|
||||
return None
|
||||
@@ -757,8 +757,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 在链表中查找值为 target 的首个结点 """
|
||||
def find(head, target):
|
||||
""" 在链表中查找值为 target 的首个结点 """
|
||||
index = 0
|
||||
while head:
|
||||
if head.val == target:
|
||||
|
||||
@@ -933,40 +933,45 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="my_list.py"
|
||||
""" 列表类简易实现 """
|
||||
class MyList:
|
||||
""" 构造方法 """
|
||||
""" 列表类简易实现 """
|
||||
def __init__(self):
|
||||
""" 构造方法 """
|
||||
self.__capacity = 10 # 列表容量
|
||||
self.__nums = [0] * self.__capacity # 数组(存储列表元素)
|
||||
self.__size = 0 # 列表长度(即当前元素数量)
|
||||
self.__extend_ratio = 2 # 每次列表扩容的倍数
|
||||
|
||||
""" 获取列表长度(即当前元素数量) """
|
||||
def size(self):
|
||||
""" 获取列表长度(即当前元素数量) """
|
||||
return self.__size
|
||||
|
||||
""" 获取列表容量 """
|
||||
def capacity(self):
|
||||
""" 获取列表容量 """
|
||||
return self.__capacity
|
||||
|
||||
""" 访问元素 """
|
||||
def get(self, index):
|
||||
""" 访问元素 """
|
||||
# 索引如果越界则抛出异常,下同
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
return self.__nums[index]
|
||||
|
||||
""" 更新元素 """
|
||||
def set(self, num, index):
|
||||
""" 更新元素 """
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
self.__nums[index] = num
|
||||
|
||||
def add(self, num):
|
||||
""" 尾部添加元素 """
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.size() == self.capacity():
|
||||
self.extend_capacity();
|
||||
self.__nums[self.__size] = num
|
||||
self.__size += 1
|
||||
|
||||
""" 中间插入(尾部添加)元素 """
|
||||
def add(self, num, index=-1):
|
||||
def insert(self, num, index):
|
||||
""" 中间插入元素 """
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
# 若不指定索引 index ,则向数组尾部添加元素
|
||||
if index == -1:
|
||||
index = self.__size
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.__size == self.capacity():
|
||||
self.extend_capacity()
|
||||
@@ -977,10 +982,10 @@ comments: true
|
||||
# 更新元素数量
|
||||
self.__size += 1
|
||||
|
||||
""" 删除元素 """
|
||||
def remove(self, index):
|
||||
""" 删除元素 """
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
num = self.nums[index]
|
||||
num = self.__nums[index]
|
||||
# 索引 i 之后的元素都向前移动一位
|
||||
for j in range(index, self.__size - 1):
|
||||
self.__nums[j] = self.__nums[j + 1]
|
||||
@@ -989,15 +994,15 @@ comments: true
|
||||
# 返回被删除元素
|
||||
return num
|
||||
|
||||
""" 列表扩容 """
|
||||
def extend_capacity(self):
|
||||
""" 列表扩容 """
|
||||
# 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组
|
||||
self.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)
|
||||
# 更新列表容量
|
||||
self.__capacity = len(self.__nums)
|
||||
|
||||
""" 返回有效长度的列表 """
|
||||
def to_array(self):
|
||||
""" 返回有效长度的列表 """
|
||||
return self.__nums[:self.__size]
|
||||
```
|
||||
|
||||
@@ -1311,7 +1316,7 @@ comments: true
|
||||
public toArray(): number[] {
|
||||
let size = this.size();
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
let nums = new Array(size);
|
||||
const nums = new Array(size);
|
||||
for (let i = 0; i < size; i++) {
|
||||
nums[i] = this.get(i);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user