mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 13:36:28 +08:00
build
This commit is contained in:
parent
c641987cd8
commit
eda4539790
8 changed files with 212 additions and 212 deletions
|
@ -872,77 +872,77 @@ comments: true
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__capacity: int = 10 # 列表容量
|
self._capacity: int = 10 # 列表容量
|
||||||
self.__arr: list[int] = [0] * self.__capacity # 数组(存储列表元素)
|
self._arr: list[int] = [0] * self._capacity # 数组(存储列表元素)
|
||||||
self.__size: int = 0 # 列表长度(即当前元素数量)
|
self._size: int = 0 # 列表长度(即当前元素数量)
|
||||||
self.__extend_ratio: int = 2 # 每次列表扩容的倍数
|
self._extend_ratio: int = 2 # 每次列表扩容的倍数
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取列表长度(即当前元素数量)"""
|
"""获取列表长度(即当前元素数量)"""
|
||||||
return self.__size
|
return self._size
|
||||||
|
|
||||||
def capacity(self) -> int:
|
def capacity(self) -> int:
|
||||||
"""获取列表容量"""
|
"""获取列表容量"""
|
||||||
return self.__capacity
|
return self._capacity
|
||||||
|
|
||||||
def get(self, index: int) -> int:
|
def get(self, index: int) -> int:
|
||||||
"""访问元素"""
|
"""访问元素"""
|
||||||
# 索引如果越界则抛出异常,下同
|
# 索引如果越界则抛出异常,下同
|
||||||
if index < 0 or index >= self.__size:
|
if index < 0 or index >= self._size:
|
||||||
raise IndexError("索引越界")
|
raise IndexError("索引越界")
|
||||||
return self.__arr[index]
|
return self._arr[index]
|
||||||
|
|
||||||
def set(self, num: int, index: int):
|
def set(self, num: int, index: int):
|
||||||
"""更新元素"""
|
"""更新元素"""
|
||||||
if index < 0 or index >= self.__size:
|
if index < 0 or index >= self._size:
|
||||||
raise IndexError("索引越界")
|
raise IndexError("索引越界")
|
||||||
self.__arr[index] = num
|
self._arr[index] = num
|
||||||
|
|
||||||
def add(self, num: int):
|
def add(self, num: int):
|
||||||
"""尾部添加元素"""
|
"""尾部添加元素"""
|
||||||
# 元素数量超出容量时,触发扩容机制
|
# 元素数量超出容量时,触发扩容机制
|
||||||
if self.size() == self.capacity():
|
if self.size() == self.capacity():
|
||||||
self.extend_capacity()
|
self.extend_capacity()
|
||||||
self.__arr[self.__size] = num
|
self._arr[self._size] = num
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def insert(self, num: int, index: int):
|
def insert(self, num: int, index: int):
|
||||||
"""中间插入元素"""
|
"""中间插入元素"""
|
||||||
if index < 0 or index >= self.__size:
|
if index < 0 or index >= self._size:
|
||||||
raise IndexError("索引越界")
|
raise IndexError("索引越界")
|
||||||
# 元素数量超出容量时,触发扩容机制
|
# 元素数量超出容量时,触发扩容机制
|
||||||
if self.__size == self.capacity():
|
if self._size == self.capacity():
|
||||||
self.extend_capacity()
|
self.extend_capacity()
|
||||||
# 将索引 index 以及之后的元素都向后移动一位
|
# 将索引 index 以及之后的元素都向后移动一位
|
||||||
for j in range(self.__size - 1, index - 1, -1):
|
for j in range(self._size - 1, index - 1, -1):
|
||||||
self.__arr[j + 1] = self.__arr[j]
|
self._arr[j + 1] = self._arr[j]
|
||||||
self.__arr[index] = num
|
self._arr[index] = num
|
||||||
# 更新元素数量
|
# 更新元素数量
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def remove(self, index: int) -> int:
|
def remove(self, index: int) -> int:
|
||||||
"""删除元素"""
|
"""删除元素"""
|
||||||
if index < 0 or index >= self.__size:
|
if index < 0 or index >= self._size:
|
||||||
raise IndexError("索引越界")
|
raise IndexError("索引越界")
|
||||||
num = self.__arr[index]
|
num = self._arr[index]
|
||||||
# 索引 i 之后的元素都向前移动一位
|
# 索引 i 之后的元素都向前移动一位
|
||||||
for j in range(index, self.__size - 1):
|
for j in range(index, self._size - 1):
|
||||||
self.__arr[j] = self.__arr[j + 1]
|
self._arr[j] = self._arr[j + 1]
|
||||||
# 更新元素数量
|
# 更新元素数量
|
||||||
self.__size -= 1
|
self._size -= 1
|
||||||
# 返回被删除元素
|
# 返回被删除元素
|
||||||
return num
|
return num
|
||||||
|
|
||||||
def extend_capacity(self):
|
def extend_capacity(self):
|
||||||
"""列表扩容"""
|
"""列表扩容"""
|
||||||
# 新建一个长度为原数组 __extend_ratio 倍的新数组,并将原数组拷贝到新数组
|
# 新建一个长度为原数组 __extend_ratio 倍的新数组,并将原数组拷贝到新数组
|
||||||
self.__arr = self.__arr + [0] * self.capacity() * (self.__extend_ratio - 1)
|
self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)
|
||||||
# 更新列表容量
|
# 更新列表容量
|
||||||
self.__capacity = len(self.__arr)
|
self._capacity = len(self._arr)
|
||||||
|
|
||||||
def to_array(self) -> list[int]:
|
def to_array(self) -> list[int]:
|
||||||
"""返回有效长度的列表"""
|
"""返回有效长度的列表"""
|
||||||
return self.__arr[: self.__size]
|
return self._arr[: self._size]
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
|
@ -122,8 +122,8 @@ $$
|
||||||
if i < 0 or j < 0:
|
if i < 0 or j < 0:
|
||||||
return inf
|
return inf
|
||||||
# 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
# 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
left = min_path_sum_dfs(grid, i - 1, j)
|
up = min_path_sum_dfs(grid, i - 1, j)
|
||||||
up = min_path_sum_dfs(grid, i, j - 1)
|
left = min_path_sum_dfs(grid, i, j - 1)
|
||||||
# 返回从左上角到 (i, j) 的最小路径代价
|
# 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return min(left, up) + grid[i][j]
|
return min(left, up) + grid[i][j]
|
||||||
```
|
```
|
||||||
|
@ -142,8 +142,8 @@ $$
|
||||||
return INT_MAX;
|
return INT_MAX;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
int left = minPathSumDFS(grid, i - 1, j);
|
int up = minPathSumDFS(grid, i - 1, j);
|
||||||
int up = minPathSumDFS(grid, i, j - 1);
|
int left = minPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
||||||
}
|
}
|
||||||
|
@ -163,8 +163,8 @@ $$
|
||||||
return Integer.MAX_VALUE;
|
return Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
int left = minPathSumDFS(grid, i - 1, j);
|
int up = minPathSumDFS(grid, i - 1, j);
|
||||||
int up = minPathSumDFS(grid, i, j - 1);
|
int left = minPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return Math.min(left, up) + grid[i][j];
|
return Math.min(left, up) + grid[i][j];
|
||||||
}
|
}
|
||||||
|
@ -184,8 +184,8 @@ $$
|
||||||
return int.MaxValue;
|
return int.MaxValue;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
int left = MinPathSumDFS(grid, i - 1, j);
|
int up = MinPathSumDFS(grid, i - 1, j);
|
||||||
int up = MinPathSumDFS(grid, i, j - 1);
|
int left = MinPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return Math.Min(left, up) + grid[i][j];
|
return Math.Min(left, up) + grid[i][j];
|
||||||
}
|
}
|
||||||
|
@ -205,8 +205,8 @@ $$
|
||||||
return math.MaxInt
|
return math.MaxInt
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
left := minPathSumDFS(grid, i-1, j)
|
up := minPathSumDFS(grid, i-1, j)
|
||||||
up := minPathSumDFS(grid, i, j-1)
|
left := minPathSumDFS(grid, i, j-1)
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return int(math.Min(float64(left), float64(up))) + grid[i][j]
|
return int(math.Min(float64(left), float64(up))) + grid[i][j]
|
||||||
}
|
}
|
||||||
|
@ -226,8 +226,8 @@ $$
|
||||||
return .max
|
return .max
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
let left = minPathSumDFS(grid: grid, i: i - 1, j: j)
|
let up = minPathSumDFS(grid: grid, i: i - 1, j: j)
|
||||||
let up = minPathSumDFS(grid: grid, i: i, j: j - 1)
|
let left = minPathSumDFS(grid: grid, i: i, j: j - 1)
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return min(left, up) + grid[i][j]
|
return min(left, up) + grid[i][j]
|
||||||
}
|
}
|
||||||
|
@ -247,8 +247,8 @@ $$
|
||||||
return Infinity;
|
return Infinity;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
const left = minPathSumDFS(grid, i - 1, j);
|
const up = minPathSumDFS(grid, i - 1, j);
|
||||||
const up = minPathSumDFS(grid, i, j - 1);
|
const left = minPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return Math.min(left, up) + grid[i][j];
|
return Math.min(left, up) + grid[i][j];
|
||||||
}
|
}
|
||||||
|
@ -272,8 +272,8 @@ $$
|
||||||
return Infinity;
|
return Infinity;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
const left = minPathSumDFS(grid, i - 1, j);
|
const up = minPathSumDFS(grid, i - 1, j);
|
||||||
const up = minPathSumDFS(grid, i, j - 1);
|
const left = minPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return Math.min(left, up) + grid[i][j];
|
return Math.min(left, up) + grid[i][j];
|
||||||
}
|
}
|
||||||
|
@ -294,8 +294,8 @@ $$
|
||||||
return BigInt.from(2).pow(31).toInt();
|
return BigInt.from(2).pow(31).toInt();
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
int left = minPathSumDFS(grid, i - 1, j);
|
int up = minPathSumDFS(grid, i - 1, j);
|
||||||
int up = minPathSumDFS(grid, i, j - 1);
|
int left = minPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return min(left, up) + grid[i][j];
|
return min(left, up) + grid[i][j];
|
||||||
}
|
}
|
||||||
|
@ -315,8 +315,8 @@ $$
|
||||||
return i32::MAX;
|
return i32::MAX;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
let left = min_path_sum_dfs(grid, i - 1, j);
|
let up = min_path_sum_dfs(grid, i - 1, j);
|
||||||
let up = min_path_sum_dfs(grid, i, j - 1);
|
let left = min_path_sum_dfs(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
std::cmp::min(left, up) + grid[i as usize][j as usize]
|
std::cmp::min(left, up) + grid[i as usize][j as usize]
|
||||||
}
|
}
|
||||||
|
@ -336,8 +336,8 @@ $$
|
||||||
return INT_MAX;
|
return INT_MAX;
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
int left = minPathSumDFS(gridCols, grid, i - 1, j);
|
int up = minPathSumDFS(gridCols, grid, i - 1, j);
|
||||||
int up = minPathSumDFS(gridCols, grid, i, j - 1);
|
int left = minPathSumDFS(gridCols, grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
||||||
}
|
}
|
||||||
|
@ -357,8 +357,8 @@ $$
|
||||||
return std.math.maxInt(i32);
|
return std.math.maxInt(i32);
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
var left = minPathSumDFS(grid, i - 1, j);
|
var up = minPathSumDFS(grid, i - 1, j);
|
||||||
var up = minPathSumDFS(grid, i, j - 1);
|
var left = minPathSumDFS(grid, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
return @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
|
return @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
|
||||||
}
|
}
|
||||||
|
@ -395,8 +395,8 @@ $$
|
||||||
if mem[i][j] != -1:
|
if mem[i][j] != -1:
|
||||||
return mem[i][j]
|
return mem[i][j]
|
||||||
# 左边和上边单元格的最小路径代价
|
# 左边和上边单元格的最小路径代价
|
||||||
left = min_path_sum_dfs_mem(grid, mem, i - 1, j)
|
up = min_path_sum_dfs_mem(grid, mem, i - 1, j)
|
||||||
up = min_path_sum_dfs_mem(grid, mem, i, j - 1)
|
left = min_path_sum_dfs_mem(grid, mem, i, j - 1)
|
||||||
# 记录并返回左上角到 (i, j) 的最小路径代价
|
# 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = min(left, up) + grid[i][j]
|
mem[i][j] = min(left, up) + grid[i][j]
|
||||||
return mem[i][j]
|
return mem[i][j]
|
||||||
|
@ -420,8 +420,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
int left = minPathSumDFSMem(grid, mem, i - 1, j);
|
int up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
int up = minPathSumDFSMem(grid, mem, i, j - 1);
|
int left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -446,8 +446,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
int left = minPathSumDFSMem(grid, mem, i - 1, j);
|
int up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
int up = minPathSumDFSMem(grid, mem, i, j - 1);
|
int left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = Math.min(left, up) + grid[i][j];
|
mem[i][j] = Math.min(left, up) + grid[i][j];
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -472,8 +472,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
int left = MinPathSumDFSMem(grid, mem, i - 1, j);
|
int up = MinPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
int up = MinPathSumDFSMem(grid, mem, i, j - 1);
|
int left = MinPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = Math.Min(left, up) + grid[i][j];
|
mem[i][j] = Math.Min(left, up) + grid[i][j];
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -498,8 +498,8 @@ $$
|
||||||
return mem[i][j]
|
return mem[i][j]
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
left := minPathSumDFSMem(grid, mem, i-1, j)
|
up := minPathSumDFSMem(grid, mem, i-1, j)
|
||||||
up := minPathSumDFSMem(grid, mem, i, j-1)
|
left := minPathSumDFSMem(grid, mem, i, j-1)
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]
|
mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]
|
||||||
return mem[i][j]
|
return mem[i][j]
|
||||||
|
@ -524,8 +524,8 @@ $$
|
||||||
return mem[i][j]
|
return mem[i][j]
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)
|
let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)
|
||||||
let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)
|
let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = min(left, up) + grid[i][j]
|
mem[i][j] = min(left, up) + grid[i][j]
|
||||||
return mem[i][j]
|
return mem[i][j]
|
||||||
|
@ -550,8 +550,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
const left = minPathSumDFSMem(grid, mem, i - 1, j);
|
const up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
const up = minPathSumDFSMem(grid, mem, i, j - 1);
|
const left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = Math.min(left, up) + grid[i][j];
|
mem[i][j] = Math.min(left, up) + grid[i][j];
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -581,8 +581,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
const left = minPathSumDFSMem(grid, mem, i - 1, j);
|
const up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
const up = minPathSumDFSMem(grid, mem, i, j - 1);
|
const left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = Math.min(left, up) + grid[i][j];
|
mem[i][j] = Math.min(left, up) + grid[i][j];
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -608,8 +608,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
int left = minPathSumDFSMem(grid, mem, i - 1, j);
|
int up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
int up = minPathSumDFSMem(grid, mem, i, j - 1);
|
int left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = min(left, up) + grid[i][j];
|
mem[i][j] = min(left, up) + grid[i][j];
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -634,8 +634,8 @@ $$
|
||||||
return mem[i as usize][j as usize];
|
return mem[i as usize][j as usize];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
let left = min_path_sum_dfs_mem(grid, mem, i - 1, j);
|
let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);
|
||||||
let up = min_path_sum_dfs_mem(grid, mem, i, j - 1);
|
let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];
|
mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];
|
||||||
mem[i as usize][j as usize]
|
mem[i as usize][j as usize]
|
||||||
|
@ -660,8 +660,8 @@ $$
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
}
|
}
|
||||||
// 左边和上边单元格的最小路径代价
|
// 左边和上边单元格的最小路径代价
|
||||||
int left = minPathSumDFSMem(gridCols, grid, mem, i - 1, j);
|
int up = minPathSumDFSMem(gridCols, grid, mem, i - 1, j);
|
||||||
int up = minPathSumDFSMem(gridCols, grid, mem, i, j - 1);
|
int left = minPathSumDFSMem(gridCols, grid, mem, i, j - 1);
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
|
||||||
return mem[i][j];
|
return mem[i][j];
|
||||||
|
@ -686,8 +686,8 @@ $$
|
||||||
return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
|
return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
|
||||||
}
|
}
|
||||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||||
var left = minPathSumDFSMem(grid, mem, i - 1, j);
|
var up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||||
var up = minPathSumDFSMem(grid, mem, i, j - 1);
|
var left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||||
// 返回从左上角到 (i, j) 的最小路径代价
|
// 返回从左上角到 (i, j) 的最小路径代价
|
||||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||||
mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
|
mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
|
||||||
|
|
|
@ -400,13 +400,13 @@ comments: true
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.front: ListNode | None = None # 头节点 front
|
self._front: ListNode | None = None # 头节点 front
|
||||||
self.rear: ListNode | None = None # 尾节点 rear
|
self._rear: ListNode | None = None # 尾节点 rear
|
||||||
self.__size: int = 0 # 双向队列的长度
|
self._size: int = 0 # 双向队列的长度
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取双向队列的长度"""
|
"""获取双向队列的长度"""
|
||||||
return self.__size
|
return self._size
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""判断双向队列是否为空"""
|
"""判断双向队列是否为空"""
|
||||||
|
@ -417,20 +417,20 @@ comments: true
|
||||||
node = ListNode(num)
|
node = ListNode(num)
|
||||||
# 若链表为空,则令 front, rear 都指向 node
|
# 若链表为空,则令 front, rear 都指向 node
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
self.front = self.rear = node
|
self._front = self._rear = node
|
||||||
# 队首入队操作
|
# 队首入队操作
|
||||||
elif is_front:
|
elif is_front:
|
||||||
# 将 node 添加至链表头部
|
# 将 node 添加至链表头部
|
||||||
self.front.prev = node
|
self._front.prev = node
|
||||||
node.next = self.front
|
node.next = self._front
|
||||||
self.front = node # 更新头节点
|
self._front = node # 更新头节点
|
||||||
# 队尾入队操作
|
# 队尾入队操作
|
||||||
else:
|
else:
|
||||||
# 将 node 添加至链表尾部
|
# 将 node 添加至链表尾部
|
||||||
self.rear.next = node
|
self._rear.next = node
|
||||||
node.prev = self.rear
|
node.prev = self._rear
|
||||||
self.rear = node # 更新尾节点
|
self._rear = node # 更新尾节点
|
||||||
self.__size += 1 # 更新队列长度
|
self._size += 1 # 更新队列长度
|
||||||
|
|
||||||
def push_first(self, num: int):
|
def push_first(self, num: int):
|
||||||
"""队首入队"""
|
"""队首入队"""
|
||||||
|
@ -446,23 +446,23 @@ comments: true
|
||||||
raise IndexError("双向队列为空")
|
raise IndexError("双向队列为空")
|
||||||
# 队首出队操作
|
# 队首出队操作
|
||||||
if is_front:
|
if is_front:
|
||||||
val: int = self.front.val # 暂存头节点值
|
val: int = self._front.val # 暂存头节点值
|
||||||
# 删除头节点
|
# 删除头节点
|
||||||
fnext: ListNode | None = self.front.next
|
fnext: ListNode | None = self._front.next
|
||||||
if fnext != None:
|
if fnext != None:
|
||||||
fnext.prev = None
|
fnext.prev = None
|
||||||
self.front.next = None
|
self._front.next = None
|
||||||
self.front = fnext # 更新头节点
|
self._front = fnext # 更新头节点
|
||||||
# 队尾出队操作
|
# 队尾出队操作
|
||||||
else:
|
else:
|
||||||
val: int = self.rear.val # 暂存尾节点值
|
val: int = self._rear.val # 暂存尾节点值
|
||||||
# 删除尾节点
|
# 删除尾节点
|
||||||
rprev: ListNode | None = self.rear.prev
|
rprev: ListNode | None = self._rear.prev
|
||||||
if rprev != None:
|
if rprev != None:
|
||||||
rprev.next = None
|
rprev.next = None
|
||||||
self.rear.prev = None
|
self._rear.prev = None
|
||||||
self.rear = rprev # 更新尾节点
|
self._rear = rprev # 更新尾节点
|
||||||
self.__size -= 1 # 更新队列长度
|
self._size -= 1 # 更新队列长度
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def pop_first(self) -> int:
|
def pop_first(self) -> int:
|
||||||
|
@ -477,17 +477,17 @@ comments: true
|
||||||
"""访问队首元素"""
|
"""访问队首元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("双向队列为空")
|
raise IndexError("双向队列为空")
|
||||||
return self.front.val
|
return self._front.val
|
||||||
|
|
||||||
def peek_last(self) -> int:
|
def peek_last(self) -> int:
|
||||||
"""访问队尾元素"""
|
"""访问队尾元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("双向队列为空")
|
raise IndexError("双向队列为空")
|
||||||
return self.rear.val
|
return self._rear.val
|
||||||
|
|
||||||
def to_array(self) -> list[int]:
|
def to_array(self) -> list[int]:
|
||||||
"""返回数组用于打印"""
|
"""返回数组用于打印"""
|
||||||
node = self.front
|
node = self._front
|
||||||
res = [0] * self.size()
|
res = [0] * self.size()
|
||||||
for i in range(self.size()):
|
for i in range(self.size()):
|
||||||
res[i] = node.val
|
res[i] = node.val
|
||||||
|
@ -2038,21 +2038,21 @@ comments: true
|
||||||
|
|
||||||
def __init__(self, capacity: int):
|
def __init__(self, capacity: int):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__nums: list[int] = [0] * capacity
|
self._nums: list[int] = [0] * capacity
|
||||||
self.__front: int = 0
|
self._front: int = 0
|
||||||
self.__size: int = 0
|
self._size: int = 0
|
||||||
|
|
||||||
def capacity(self) -> int:
|
def capacity(self) -> int:
|
||||||
"""获取双向队列的容量"""
|
"""获取双向队列的容量"""
|
||||||
return len(self.__nums)
|
return len(self._nums)
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取双向队列的长度"""
|
"""获取双向队列的长度"""
|
||||||
return self.__size
|
return self._size
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""判断双向队列是否为空"""
|
"""判断双向队列是否为空"""
|
||||||
return self.__size == 0
|
return self._size == 0
|
||||||
|
|
||||||
def index(self, i: int) -> int:
|
def index(self, i: int) -> int:
|
||||||
"""计算环形数组索引"""
|
"""计算环形数组索引"""
|
||||||
|
@ -2063,61 +2063,61 @@ comments: true
|
||||||
|
|
||||||
def push_first(self, num: int):
|
def push_first(self, num: int):
|
||||||
"""队首入队"""
|
"""队首入队"""
|
||||||
if self.__size == self.capacity():
|
if self._size == self.capacity():
|
||||||
print("双向队列已满")
|
print("双向队列已满")
|
||||||
return
|
return
|
||||||
# 队首指针向左移动一位
|
# 队首指针向左移动一位
|
||||||
# 通过取余操作,实现 front 越过数组头部后回到尾部
|
# 通过取余操作,实现 front 越过数组头部后回到尾部
|
||||||
self.__front = self.index(self.__front - 1)
|
self._front = self.index(self._front - 1)
|
||||||
# 将 num 添加至队首
|
# 将 num 添加至队首
|
||||||
self.__nums[self.__front] = num
|
self._nums[self._front] = num
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def push_last(self, num: int):
|
def push_last(self, num: int):
|
||||||
"""队尾入队"""
|
"""队尾入队"""
|
||||||
if self.__size == self.capacity():
|
if self._size == self.capacity():
|
||||||
print("双向队列已满")
|
print("双向队列已满")
|
||||||
return
|
return
|
||||||
# 计算尾指针,指向队尾索引 + 1
|
# 计算尾指针,指向队尾索引 + 1
|
||||||
rear = self.index(self.__front + self.__size)
|
rear = self.index(self._front + self._size)
|
||||||
# 将 num 添加至队尾
|
# 将 num 添加至队尾
|
||||||
self.__nums[rear] = num
|
self._nums[rear] = num
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def pop_first(self) -> int:
|
def pop_first(self) -> int:
|
||||||
"""队首出队"""
|
"""队首出队"""
|
||||||
num = self.peek_first()
|
num = self.peek_first()
|
||||||
# 队首指针向后移动一位
|
# 队首指针向后移动一位
|
||||||
self.__front = self.index(self.__front + 1)
|
self._front = self.index(self._front + 1)
|
||||||
self.__size -= 1
|
self._size -= 1
|
||||||
return num
|
return num
|
||||||
|
|
||||||
def pop_last(self) -> int:
|
def pop_last(self) -> int:
|
||||||
"""队尾出队"""
|
"""队尾出队"""
|
||||||
num = self.peek_last()
|
num = self.peek_last()
|
||||||
self.__size -= 1
|
self._size -= 1
|
||||||
return num
|
return num
|
||||||
|
|
||||||
def peek_first(self) -> int:
|
def peek_first(self) -> int:
|
||||||
"""访问队首元素"""
|
"""访问队首元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("双向队列为空")
|
raise IndexError("双向队列为空")
|
||||||
return self.__nums[self.__front]
|
return self._nums[self._front]
|
||||||
|
|
||||||
def peek_last(self) -> int:
|
def peek_last(self) -> int:
|
||||||
"""访问队尾元素"""
|
"""访问队尾元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("双向队列为空")
|
raise IndexError("双向队列为空")
|
||||||
# 计算尾元素索引
|
# 计算尾元素索引
|
||||||
last = self.index(self.__front + self.__size - 1)
|
last = self.index(self._front + self._size - 1)
|
||||||
return self.__nums[last]
|
return self._nums[last]
|
||||||
|
|
||||||
def to_array(self) -> list[int]:
|
def to_array(self) -> list[int]:
|
||||||
"""返回数组用于打印"""
|
"""返回数组用于打印"""
|
||||||
# 仅转换有效长度范围内的列表元素
|
# 仅转换有效长度范围内的列表元素
|
||||||
res = []
|
res = []
|
||||||
for i in range(self.__size):
|
for i in range(self._size):
|
||||||
res.append(self.__nums[self.index(self.__front + i)])
|
res.append(self._nums[self.index(self._front + i)])
|
||||||
return res
|
return res
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -345,50 +345,50 @@ comments: true
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__front: ListNode | None = None # 头节点 front
|
self._front: ListNode | None = None # 头节点 front
|
||||||
self.__rear: ListNode | None = None # 尾节点 rear
|
self._rear: ListNode | None = None # 尾节点 rear
|
||||||
self.__size: int = 0
|
self._size: int = 0
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取队列的长度"""
|
"""获取队列的长度"""
|
||||||
return self.__size
|
return self._size
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""判断队列是否为空"""
|
"""判断队列是否为空"""
|
||||||
return not self.__front
|
return not self._front
|
||||||
|
|
||||||
def push(self, num: int):
|
def push(self, num: int):
|
||||||
"""入队"""
|
"""入队"""
|
||||||
# 尾节点后添加 num
|
# 尾节点后添加 num
|
||||||
node = ListNode(num)
|
node = ListNode(num)
|
||||||
# 如果队列为空,则令头、尾节点都指向该节点
|
# 如果队列为空,则令头、尾节点都指向该节点
|
||||||
if self.__front is None:
|
if self._front is None:
|
||||||
self.__front = node
|
self._front = node
|
||||||
self.__rear = node
|
self._rear = node
|
||||||
# 如果队列不为空,则将该节点添加到尾节点后
|
# 如果队列不为空,则将该节点添加到尾节点后
|
||||||
else:
|
else:
|
||||||
self.__rear.next = node
|
self._rear.next = node
|
||||||
self.__rear = node
|
self._rear = node
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""出队"""
|
"""出队"""
|
||||||
num = self.peek()
|
num = self.peek()
|
||||||
# 删除头节点
|
# 删除头节点
|
||||||
self.__front = self.__front.next
|
self._front = self._front.next
|
||||||
self.__size -= 1
|
self._size -= 1
|
||||||
return num
|
return num
|
||||||
|
|
||||||
def peek(self) -> int:
|
def peek(self) -> int:
|
||||||
"""访问队首元素"""
|
"""访问队首元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("队列为空")
|
raise IndexError("队列为空")
|
||||||
return self.__front.val
|
return self._front.val
|
||||||
|
|
||||||
def to_list(self) -> list[int]:
|
def to_list(self) -> list[int]:
|
||||||
"""转化为列表用于打印"""
|
"""转化为列表用于打印"""
|
||||||
queue = []
|
queue = []
|
||||||
temp = self.__front
|
temp = self._front
|
||||||
while temp:
|
while temp:
|
||||||
queue.append(temp.val)
|
queue.append(temp.val)
|
||||||
temp = temp.next
|
temp = temp.next
|
||||||
|
@ -1240,53 +1240,53 @@ comments: true
|
||||||
|
|
||||||
def __init__(self, size: int):
|
def __init__(self, size: int):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__nums: list[int] = [0] * size # 用于存储队列元素的数组
|
self._nums: list[int] = [0] * size # 用于存储队列元素的数组
|
||||||
self.__front: int = 0 # 队首指针,指向队首元素
|
self._front: int = 0 # 队首指针,指向队首元素
|
||||||
self.__size: int = 0 # 队列长度
|
self._size: int = 0 # 队列长度
|
||||||
|
|
||||||
def capacity(self) -> int:
|
def capacity(self) -> int:
|
||||||
"""获取队列的容量"""
|
"""获取队列的容量"""
|
||||||
return len(self.__nums)
|
return len(self._nums)
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取队列的长度"""
|
"""获取队列的长度"""
|
||||||
return self.__size
|
return self._size
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""判断队列是否为空"""
|
"""判断队列是否为空"""
|
||||||
return self.__size == 0
|
return self._size == 0
|
||||||
|
|
||||||
def push(self, num: int):
|
def push(self, num: int):
|
||||||
"""入队"""
|
"""入队"""
|
||||||
if self.__size == self.capacity():
|
if self._size == self.capacity():
|
||||||
raise IndexError("队列已满")
|
raise IndexError("队列已满")
|
||||||
# 计算尾指针,指向队尾索引 + 1
|
# 计算尾指针,指向队尾索引 + 1
|
||||||
# 通过取余操作,实现 rear 越过数组尾部后回到头部
|
# 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||||
rear: int = (self.__front + self.__size) % self.capacity()
|
rear: int = (self._front + self._size) % self.capacity()
|
||||||
# 将 num 添加至队尾
|
# 将 num 添加至队尾
|
||||||
self.__nums[rear] = num
|
self._nums[rear] = num
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""出队"""
|
"""出队"""
|
||||||
num: int = self.peek()
|
num: int = self.peek()
|
||||||
# 队首指针向后移动一位,若越过尾部则返回到数组头部
|
# 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||||
self.__front = (self.__front + 1) % self.capacity()
|
self._front = (self._front + 1) % self.capacity()
|
||||||
self.__size -= 1
|
self._size -= 1
|
||||||
return num
|
return num
|
||||||
|
|
||||||
def peek(self) -> int:
|
def peek(self) -> int:
|
||||||
"""访问队首元素"""
|
"""访问队首元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("队列为空")
|
raise IndexError("队列为空")
|
||||||
return self.__nums[self.__front]
|
return self._nums[self._front]
|
||||||
|
|
||||||
def to_list(self) -> list[int]:
|
def to_list(self) -> list[int]:
|
||||||
"""返回列表用于打印"""
|
"""返回列表用于打印"""
|
||||||
res = [0] * self.size()
|
res = [0] * self.size()
|
||||||
j: int = self.__front
|
j: int = self._front
|
||||||
for i in range(self.size()):
|
for i in range(self.size()):
|
||||||
res[i] = self.__nums[(j % self.capacity())]
|
res[i] = self._nums[(j % self.capacity())]
|
||||||
j += 1
|
j += 1
|
||||||
return res
|
return res
|
||||||
```
|
```
|
||||||
|
|
|
@ -345,41 +345,41 @@ comments: true
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__peek: ListNode | None = None
|
self._peek: ListNode | None = None
|
||||||
self.__size: int = 0
|
self._size: int = 0
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取栈的长度"""
|
"""获取栈的长度"""
|
||||||
return self.__size
|
return self._size
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""判断栈是否为空"""
|
"""判断栈是否为空"""
|
||||||
return not self.__peek
|
return not self._peek
|
||||||
|
|
||||||
def push(self, val: int):
|
def push(self, val: int):
|
||||||
"""入栈"""
|
"""入栈"""
|
||||||
node = ListNode(val)
|
node = ListNode(val)
|
||||||
node.next = self.__peek
|
node.next = self._peek
|
||||||
self.__peek = node
|
self._peek = node
|
||||||
self.__size += 1
|
self._size += 1
|
||||||
|
|
||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""出栈"""
|
"""出栈"""
|
||||||
num = self.peek()
|
num = self.peek()
|
||||||
self.__peek = self.__peek.next
|
self._peek = self._peek.next
|
||||||
self.__size -= 1
|
self._size -= 1
|
||||||
return num
|
return num
|
||||||
|
|
||||||
def peek(self) -> int:
|
def peek(self) -> int:
|
||||||
"""访问栈顶元素"""
|
"""访问栈顶元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("栈为空")
|
raise IndexError("栈为空")
|
||||||
return self.__peek.val
|
return self._peek.val
|
||||||
|
|
||||||
def to_list(self) -> list[int]:
|
def to_list(self) -> list[int]:
|
||||||
"""转化为列表用于打印"""
|
"""转化为列表用于打印"""
|
||||||
arr = []
|
arr = []
|
||||||
node = self.__peek
|
node = self._peek
|
||||||
while node:
|
while node:
|
||||||
arr.append(node.val)
|
arr.append(node.val)
|
||||||
node = node.next
|
node = node.next
|
||||||
|
@ -1111,35 +1111,35 @@ comments: true
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__stack: list[int] = []
|
self._stack: list[int] = []
|
||||||
|
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
"""获取栈的长度"""
|
"""获取栈的长度"""
|
||||||
return len(self.__stack)
|
return len(self._stack)
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""判断栈是否为空"""
|
"""判断栈是否为空"""
|
||||||
return self.__stack == []
|
return self._stack == []
|
||||||
|
|
||||||
def push(self, item: int):
|
def push(self, item: int):
|
||||||
"""入栈"""
|
"""入栈"""
|
||||||
self.__stack.append(item)
|
self._stack.append(item)
|
||||||
|
|
||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""出栈"""
|
"""出栈"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("栈为空")
|
raise IndexError("栈为空")
|
||||||
return self.__stack.pop()
|
return self._stack.pop()
|
||||||
|
|
||||||
def peek(self) -> int:
|
def peek(self) -> int:
|
||||||
"""访问栈顶元素"""
|
"""访问栈顶元素"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError("栈为空")
|
raise IndexError("栈为空")
|
||||||
return self.__stack[-1]
|
return self._stack[-1]
|
||||||
|
|
||||||
def to_list(self) -> list[int]:
|
def to_list(self) -> list[int]:
|
||||||
"""返回列表用于打印"""
|
"""返回列表用于打印"""
|
||||||
return self.__stack
|
return self._stack
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
|
@ -151,18 +151,18 @@ comments: true
|
||||||
|
|
||||||
def __init__(self, arr: list[int | None]):
|
def __init__(self, arr: list[int | None]):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
self.__tree = list(arr)
|
self._tree = list(arr)
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
"""节点数量"""
|
"""节点数量"""
|
||||||
return len(self.__tree)
|
return len(self._tree)
|
||||||
|
|
||||||
def val(self, i: int) -> int:
|
def val(self, i: int) -> int:
|
||||||
"""获取索引为 i 节点的值"""
|
"""获取索引为 i 节点的值"""
|
||||||
# 若索引越界,则返回 None ,代表空位
|
# 若索引越界,则返回 None ,代表空位
|
||||||
if i < 0 or i >= self.size():
|
if i < 0 or i >= self.size():
|
||||||
return None
|
return None
|
||||||
return self.__tree[i]
|
return self._tree[i]
|
||||||
|
|
||||||
def left(self, i: int) -> int | None:
|
def left(self, i: int) -> int | None:
|
||||||
"""获取索引为 i 节点的左子节点的索引"""
|
"""获取索引为 i 节点的左子节点的索引"""
|
||||||
|
@ -185,18 +185,18 @@ comments: true
|
||||||
self.res.append(self.val(i))
|
self.res.append(self.val(i))
|
||||||
return self.res
|
return self.res
|
||||||
|
|
||||||
def __dfs(self, i: int, order: str):
|
def dfs(self, i: int, order: str):
|
||||||
"""深度优先遍历"""
|
"""深度优先遍历"""
|
||||||
if self.val(i) is None:
|
if self.val(i) is None:
|
||||||
return
|
return
|
||||||
# 前序遍历
|
# 前序遍历
|
||||||
if order == "pre":
|
if order == "pre":
|
||||||
self.res.append(self.val(i))
|
self.res.append(self.val(i))
|
||||||
self.__dfs(self.left(i), order)
|
self.dfs(self.left(i), order)
|
||||||
# 中序遍历
|
# 中序遍历
|
||||||
if order == "in":
|
if order == "in":
|
||||||
self.res.append(self.val(i))
|
self.res.append(self.val(i))
|
||||||
self.__dfs(self.right(i), order)
|
self.dfs(self.right(i), order)
|
||||||
# 后序遍历
|
# 后序遍历
|
||||||
if order == "post":
|
if order == "post":
|
||||||
self.res.append(self.val(i))
|
self.res.append(self.val(i))
|
||||||
|
@ -204,19 +204,19 @@ comments: true
|
||||||
def pre_order(self) -> list[int]:
|
def pre_order(self) -> list[int]:
|
||||||
"""前序遍历"""
|
"""前序遍历"""
|
||||||
self.res = []
|
self.res = []
|
||||||
self.__dfs(0, order="pre")
|
self.dfs(0, order="pre")
|
||||||
return self.res
|
return self.res
|
||||||
|
|
||||||
def in_order(self) -> list[int]:
|
def in_order(self) -> list[int]:
|
||||||
"""中序遍历"""
|
"""中序遍历"""
|
||||||
self.res = []
|
self.res = []
|
||||||
self.__dfs(0, order="in")
|
self.dfs(0, order="in")
|
||||||
return self.res
|
return self.res
|
||||||
|
|
||||||
def post_order(self) -> list[int]:
|
def post_order(self) -> list[int]:
|
||||||
"""后序遍历"""
|
"""后序遍历"""
|
||||||
self.res = []
|
self.res = []
|
||||||
self.__dfs(0, order="post")
|
self.dfs(0, order="post")
|
||||||
return self.res
|
return self.res
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -229,7 +229,7 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||||
return node.height
|
return node.height
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
def __update_height(self, node: TreeNode | None):
|
def update_height(self, node: TreeNode | None):
|
||||||
"""更新节点高度"""
|
"""更新节点高度"""
|
||||||
# 节点高度等于最高子树高度 + 1
|
# 节点高度等于最高子树高度 + 1
|
||||||
node.height = max([self.height(node.left), self.height(node.right)]) + 1
|
node.height = max([self.height(node.left), self.height(node.right)]) + 1
|
||||||
|
@ -636,7 +636,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="avl_tree.py"
|
```python title="avl_tree.py"
|
||||||
def __right_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
def right_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
||||||
"""右旋操作"""
|
"""右旋操作"""
|
||||||
child = node.left
|
child = node.left
|
||||||
grand_child = child.right
|
grand_child = child.right
|
||||||
|
@ -644,8 +644,8 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||||
child.right = node
|
child.right = node
|
||||||
node.left = grand_child
|
node.left = grand_child
|
||||||
# 更新节点高度
|
# 更新节点高度
|
||||||
self.__update_height(node)
|
self.update_height(node)
|
||||||
self.__update_height(child)
|
self.update_height(child)
|
||||||
# 返回旋转后子树的根节点
|
# 返回旋转后子树的根节点
|
||||||
return child
|
return child
|
||||||
```
|
```
|
||||||
|
@ -873,7 +873,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="avl_tree.py"
|
```python title="avl_tree.py"
|
||||||
def __left_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
||||||
"""左旋操作"""
|
"""左旋操作"""
|
||||||
child = node.right
|
child = node.right
|
||||||
grand_child = child.left
|
grand_child = child.left
|
||||||
|
@ -881,8 +881,8 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||||
child.left = node
|
child.left = node
|
||||||
node.right = grand_child
|
node.right = grand_child
|
||||||
# 更新节点高度
|
# 更新节点高度
|
||||||
self.__update_height(node)
|
self.update_height(node)
|
||||||
self.__update_height(child)
|
self.update_height(child)
|
||||||
# 返回旋转后子树的根节点
|
# 返回旋转后子树的根节点
|
||||||
return child
|
return child
|
||||||
```
|
```
|
||||||
|
@ -1135,7 +1135,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="avl_tree.py"
|
```python title="avl_tree.py"
|
||||||
def __rotate(self, node: TreeNode | None) -> TreeNode | None:
|
def rotate(self, node: TreeNode | None) -> TreeNode | None:
|
||||||
"""执行旋转操作,使该子树重新恢复平衡"""
|
"""执行旋转操作,使该子树重新恢复平衡"""
|
||||||
# 获取节点 node 的平衡因子
|
# 获取节点 node 的平衡因子
|
||||||
balance_factor = self.balance_factor(node)
|
balance_factor = self.balance_factor(node)
|
||||||
|
@ -1143,20 +1143,20 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||||
if balance_factor > 1:
|
if balance_factor > 1:
|
||||||
if self.balance_factor(node.left) >= 0:
|
if self.balance_factor(node.left) >= 0:
|
||||||
# 右旋
|
# 右旋
|
||||||
return self.__right_rotate(node)
|
return self.right_rotate(node)
|
||||||
else:
|
else:
|
||||||
# 先左旋后右旋
|
# 先左旋后右旋
|
||||||
node.left = self.__left_rotate(node.left)
|
node.left = self.left_rotate(node.left)
|
||||||
return self.__right_rotate(node)
|
return self.right_rotate(node)
|
||||||
# 右偏树
|
# 右偏树
|
||||||
elif balance_factor < -1:
|
elif balance_factor < -1:
|
||||||
if self.balance_factor(node.right) <= 0:
|
if self.balance_factor(node.right) <= 0:
|
||||||
# 左旋
|
# 左旋
|
||||||
return self.__left_rotate(node)
|
return self.left_rotate(node)
|
||||||
else:
|
else:
|
||||||
# 先右旋后左旋
|
# 先右旋后左旋
|
||||||
node.right = self.__right_rotate(node.right)
|
node.right = self.right_rotate(node.right)
|
||||||
return self.__left_rotate(node)
|
return self.left_rotate(node)
|
||||||
# 平衡树,无须旋转,直接返回
|
# 平衡树,无须旋转,直接返回
|
||||||
return node
|
return node
|
||||||
```
|
```
|
||||||
|
@ -1552,24 +1552,24 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||||
```python title="avl_tree.py"
|
```python title="avl_tree.py"
|
||||||
def insert(self, val):
|
def insert(self, val):
|
||||||
"""插入节点"""
|
"""插入节点"""
|
||||||
self.root = self.__insert_helper(self.root, val)
|
self._root = self.insert_helper(self._root, val)
|
||||||
|
|
||||||
def __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
|
def insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
|
||||||
"""递归插入节点(辅助方法)"""
|
"""递归插入节点(辅助方法)"""
|
||||||
if node is None:
|
if node is None:
|
||||||
return TreeNode(val)
|
return TreeNode(val)
|
||||||
# 1. 查找插入位置,并插入节点
|
# 1. 查找插入位置,并插入节点
|
||||||
if val < node.val:
|
if val < node.val:
|
||||||
node.left = self.__insert_helper(node.left, val)
|
node.left = self.insert_helper(node.left, val)
|
||||||
elif val > node.val:
|
elif val > node.val:
|
||||||
node.right = self.__insert_helper(node.right, val)
|
node.right = self.insert_helper(node.right, val)
|
||||||
else:
|
else:
|
||||||
# 重复节点不插入,直接返回
|
# 重复节点不插入,直接返回
|
||||||
return node
|
return node
|
||||||
# 更新节点高度
|
# 更新节点高度
|
||||||
self.__update_height(node)
|
self.update_height(node)
|
||||||
# 2. 执行旋转操作,使该子树重新恢复平衡
|
# 2. 执行旋转操作,使该子树重新恢复平衡
|
||||||
return self.__rotate(node)
|
return self.rotate(node)
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C++"
|
=== "C++"
|
||||||
|
@ -1904,17 +1904,17 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||||
```python title="avl_tree.py"
|
```python title="avl_tree.py"
|
||||||
def remove(self, val: int):
|
def remove(self, val: int):
|
||||||
"""删除节点"""
|
"""删除节点"""
|
||||||
self.root = self.__remove_helper(self.root, val)
|
self._root = self.remove_helper(self._root, val)
|
||||||
|
|
||||||
def __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
|
def remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
|
||||||
"""递归删除节点(辅助方法)"""
|
"""递归删除节点(辅助方法)"""
|
||||||
if node is None:
|
if node is None:
|
||||||
return None
|
return None
|
||||||
# 1. 查找节点,并删除之
|
# 1. 查找节点,并删除之
|
||||||
if val < node.val:
|
if val < node.val:
|
||||||
node.left = self.__remove_helper(node.left, val)
|
node.left = self.remove_helper(node.left, val)
|
||||||
elif val > node.val:
|
elif val > node.val:
|
||||||
node.right = self.__remove_helper(node.right, val)
|
node.right = self.remove_helper(node.right, val)
|
||||||
else:
|
else:
|
||||||
if node.left is None or node.right is None:
|
if node.left is None or node.right is None:
|
||||||
child = node.left or node.right
|
child = node.left or node.right
|
||||||
|
@ -1929,12 +1929,12 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||||
temp = node.right
|
temp = node.right
|
||||||
while temp.left is not None:
|
while temp.left is not None:
|
||||||
temp = temp.left
|
temp = temp.left
|
||||||
node.right = self.__remove_helper(node.right, temp.val)
|
node.right = self.remove_helper(node.right, temp.val)
|
||||||
node.val = temp.val
|
node.val = temp.val
|
||||||
# 更新节点高度
|
# 更新节点高度
|
||||||
self.__update_height(node)
|
self.update_height(node)
|
||||||
# 2. 执行旋转操作,使该子树重新恢复平衡
|
# 2. 执行旋转操作,使该子树重新恢复平衡
|
||||||
return self.__rotate(node)
|
return self.rotate(node)
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
|
@ -46,7 +46,7 @@ comments: true
|
||||||
```python title="binary_search_tree.py"
|
```python title="binary_search_tree.py"
|
||||||
def search(self, num: int) -> TreeNode | None:
|
def search(self, num: int) -> TreeNode | None:
|
||||||
"""查找节点"""
|
"""查找节点"""
|
||||||
cur = self.__root
|
cur = self._root
|
||||||
# 循环查找,越过叶节点后跳出
|
# 循环查找,越过叶节点后跳出
|
||||||
while cur is not None:
|
while cur is not None:
|
||||||
# 目标节点在 cur 的右子树中
|
# 目标节点在 cur 的右子树中
|
||||||
|
@ -340,11 +340,11 @@ comments: true
|
||||||
def insert(self, num: int):
|
def insert(self, num: int):
|
||||||
"""插入节点"""
|
"""插入节点"""
|
||||||
# 若树为空,则初始化根节点
|
# 若树为空,则初始化根节点
|
||||||
if self.__root is None:
|
if self._root is None:
|
||||||
self.__root = TreeNode(num)
|
self._root = TreeNode(num)
|
||||||
return
|
return
|
||||||
# 循环查找,越过叶节点后跳出
|
# 循环查找,越过叶节点后跳出
|
||||||
cur, pre = self.__root, None
|
cur, pre = self._root, None
|
||||||
while cur is not None:
|
while cur is not None:
|
||||||
# 找到重复节点,直接返回
|
# 找到重复节点,直接返回
|
||||||
if cur.val == num:
|
if cur.val == num:
|
||||||
|
@ -792,10 +792,10 @@ comments: true
|
||||||
def remove(self, num: int):
|
def remove(self, num: int):
|
||||||
"""删除节点"""
|
"""删除节点"""
|
||||||
# 若树为空,直接提前返回
|
# 若树为空,直接提前返回
|
||||||
if self.__root is None:
|
if self._root is None:
|
||||||
return
|
return
|
||||||
# 循环查找,越过叶节点后跳出
|
# 循环查找,越过叶节点后跳出
|
||||||
cur, pre = self.__root, None
|
cur, pre = self._root, None
|
||||||
while cur is not None:
|
while cur is not None:
|
||||||
# 找到待删除节点,跳出循环
|
# 找到待删除节点,跳出循环
|
||||||
if cur.val == num:
|
if cur.val == num:
|
||||||
|
@ -816,14 +816,14 @@ comments: true
|
||||||
# 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
# 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
||||||
child = cur.left or cur.right
|
child = cur.left or cur.right
|
||||||
# 删除节点 cur
|
# 删除节点 cur
|
||||||
if cur != self.__root:
|
if cur != self._root:
|
||||||
if pre.left == cur:
|
if pre.left == cur:
|
||||||
pre.left = child
|
pre.left = child
|
||||||
else:
|
else:
|
||||||
pre.right = child
|
pre.right = child
|
||||||
else:
|
else:
|
||||||
# 若删除节点为根节点,则重新指定根节点
|
# 若删除节点为根节点,则重新指定根节点
|
||||||
self.__root = child
|
self._root = child
|
||||||
# 子节点数量 = 2
|
# 子节点数量 = 2
|
||||||
else:
|
else:
|
||||||
# 获取中序遍历中 cur 的下一个节点
|
# 获取中序遍历中 cur 的下一个节点
|
||||||
|
|
Loading…
Reference in a new issue