mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
Fine tune
This commit is contained in:
@@ -314,7 +314,7 @@ comments: true
|
||||
list.Insert(3, 6);
|
||||
|
||||
/* 删除元素 */
|
||||
list.Remove(3);
|
||||
list.RemoveAt(3);
|
||||
```
|
||||
|
||||
**遍历列表。** 与数组一样,列表可以使用索引遍历,也可以使用 `for-each` 直接遍历。
|
||||
@@ -376,9 +376,9 @@ comments: true
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0
|
||||
for range list {
|
||||
count++
|
||||
}
|
||||
for range list {
|
||||
count++
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -1148,7 +1148,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int get(int index)
|
||||
public int Get(int index)
|
||||
{
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index >= size)
|
||||
@@ -1157,7 +1157,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void set(int index, int num)
|
||||
public void Set(int index, int num)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
@@ -1165,24 +1165,24 @@ comments: true
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void add(int num)
|
||||
public void Add(int num)
|
||||
{
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
extendCapacity();
|
||||
ExtendCapacity();
|
||||
nums[size] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void insert(int index, int num)
|
||||
public void Insert(int index, int num)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
extendCapacity();
|
||||
ExtendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--)
|
||||
{
|
||||
@@ -1194,7 +1194,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int remove(int index)
|
||||
public int Remove(int index)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
@@ -1211,25 +1211,12 @@ comments: true
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity()
|
||||
public void ExtendCapacity()
|
||||
{
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, Capacity() * extendRatio);
|
||||
// 更新列表容量
|
||||
capacity = nums.Length;
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
public int[] toArray()
|
||||
{
|
||||
int size = Size();
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] nums = new int[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
nums[i] = get(i);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -365,7 +365,7 @@ comments: true
|
||||
for (int i = nums.Length - 1; i > 0; i--)
|
||||
{
|
||||
bool flag = false; // 初始化标志位
|
||||
// 内循环:冒泡操作
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
if (nums[j] > nums[j + 1])
|
||||
|
||||
@@ -151,7 +151,7 @@ comments: true
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > bas)
|
||||
{
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = bas; // 2. 将 base 赋值到正确位置
|
||||
|
||||
@@ -352,7 +352,7 @@ comments: true
|
||||
void merge(int[] nums, int left, int mid, int right)
|
||||
{
|
||||
// 初始化辅助数组
|
||||
int[] tmp = nums[left..(right + 1)];//Array.CopyOfRange(nums, left, right + 1);
|
||||
int[] tmp = nums[left..(right + 1)];
|
||||
// 左子数组的起始索引和结束索引
|
||||
int leftStart = left - left, leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
|
||||
@@ -660,7 +660,6 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 返回子树的根节点
|
||||
return node;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### 删除结点
|
||||
@@ -765,7 +764,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
}
|
||||
|
||||
/* 递归删除结点(辅助函数) */
|
||||
private TreeNode? removeHelper(TreeNode? node, int? val)
|
||||
private TreeNode? removeHelper(TreeNode? node, int val)
|
||||
{
|
||||
if (node == null) return null;
|
||||
/* 1. 查找结点,并删除之 */
|
||||
@@ -789,8 +788,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
{
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
TreeNode? temp = minNode(node.right);
|
||||
node.right = removeHelper(node.right, temp?.val);
|
||||
node.val = temp?.val;
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新结点高度
|
||||
|
||||
@@ -692,7 +692,7 @@ comments: true
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
/* 删除结点 */
|
||||
TreeNode? remove(int? num)
|
||||
TreeNode? remove(int num)
|
||||
{
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
@@ -724,7 +724,6 @@ comments: true
|
||||
{
|
||||
pre.right = child;
|
||||
}
|
||||
|
||||
}
|
||||
// 子结点数量 = 2
|
||||
else
|
||||
@@ -733,7 +732,7 @@ comments: true
|
||||
TreeNode? nex = min(cur.right);
|
||||
if (nex != null)
|
||||
{
|
||||
int? tmp = nex.val;
|
||||
int tmp = nex.val;
|
||||
// 递归删除结点 nex
|
||||
remove(nex.val);
|
||||
// 将 nex 的值复制给 cur
|
||||
@@ -742,7 +741,7 @@ comments: true
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
|
||||
/* 获取最小结点 */
|
||||
TreeNode? min(TreeNode? root)
|
||||
{
|
||||
|
||||
@@ -156,11 +156,11 @@ comments: true
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<int?> list = new();
|
||||
List<int> list = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存结点值
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存结点值
|
||||
if (node.left != null)
|
||||
queue.Enqueue(node.left); // 左子结点入队
|
||||
if (node.right != null)
|
||||
|
||||
Reference in New Issue
Block a user