This commit is contained in:
krahets
2025-03-14 17:51:03 +08:00
parent c458348df2
commit e81bc45c43
31 changed files with 392 additions and 394 deletions

View File

@@ -1963,8 +1963,7 @@ comments: true
impl MyList {
/* 构造方法 */
pub fn new(capacity: usize) -> Self {
let mut vec = Vec::new();
vec.resize(capacity, 0);
let mut vec = vec![0; capacity];
Self {
arr: vec,
capacity,
@@ -2036,7 +2035,7 @@ comments: true
};
let num = self.arr[index];
// 将将索引 index 之后的元素都向前移动一位
for j in (index..self.size - 1) {
for j in index..self.size - 1 {
self.arr[j] = self.arr[j + 1];
}
// 更新元素数量
@@ -2055,7 +2054,7 @@ comments: true
}
/* 将列表转换为数组 */
pub fn to_array(&mut self) -> Vec<i32> {
pub fn to_array(&self) -> Vec<i32> {
// 仅转换有效长度范围内的列表元素
let mut arr = Vec::new();
for i in 0..self.size {

View File

@@ -15,7 +15,7 @@ comments: true
- 子集和问题的目标是在给定集合中找到和为目标值的所有子集。集合不区分元素顺序,而搜索过程会输出所有顺序的结果,产生重复子集。我们在回溯前将数据进行排序,并设置一个变量来指示每一轮的遍历起始点,从而将生成重复子集的搜索分支进行剪枝。
- 对于子集和问题,数组中的相等元素会产生重复集合。我们利用数组已排序的前置条件,通过判断相邻元素是否相等实现剪枝,从而确保相等元素在每轮中只能被选中一次。
- $n$ 皇后问题旨在寻找将 $n$ 个皇后放置到 $n \times n$ 尺寸棋盘上的方案,要求所有皇后两两之间无法攻击对方。该问题的约束条件有行约束、列约束、主对角线和次对角线约束。为满足行约束,我们采用按行放置的策略,保证每一行放置一个皇后。
- 列约束和对角线约束的处理方式类似。对于列约束,我们利用一个数组来记录每一列是否有皇后,从而指示选中的格子是否合法。对于对角线约束,我们借助两个数组来分别记录该主、次对角线上是否存在皇后;难点在于找处在同一主(副)对角线上格子满足的行列索引规律。
- 列约束和对角线约束的处理方式类似。对于列约束,我们利用一个数组来记录每一列是否有皇后,从而指示选中的格子是否合法。对于对角线约束,我们借助两个数组来分别记录该主、次对角线上是否存在皇后;难点在于找处在同一主(副)对角线上格子满足的行列索引规律。
### 2. &nbsp; Q & A

View File

@@ -354,7 +354,7 @@ index = hash(key) % capacity
for (const c of key) {
hash ^= c.charCodeAt(0);
}
return hash & MODULUS;
return hash % MODULUS;
}
/* 旋转哈希 */
@@ -398,7 +398,7 @@ index = hash(key) % capacity
for (const c of key) {
hash ^= c.charCodeAt(0);
}
return hash & MODULUS;
return hash % MODULUS;
}
/* 旋转哈希 */

View File

@@ -1025,10 +1025,10 @@ comments: true
```rust title="hash_map_chaining.rs"
/* 链式地址哈希表 */
struct HashMapChaining {
size: i32,
capacity: i32,
size: usize,
capacity: usize,
load_thres: f32,
extend_ratio: i32,
extend_ratio: usize,
buckets: Vec<Vec<Pair>>,
}
@@ -1046,7 +1046,7 @@ comments: true
/* 哈希函数 */
fn hash_func(&self, key: i32) -> usize {
key as usize % self.capacity as usize
key as usize % self.capacity
}
/* 负载因子 */
@@ -1057,12 +1057,11 @@ comments: true
/* 删除操作 */
fn remove(&mut self, key: i32) -> Option<String> {
let index = self.hash_func(key);
let bucket = &mut self.buckets[index];
// 遍历桶,从中删除键值对
for i in 0..bucket.len() {
if bucket[i].key == key {
let pair = bucket.remove(i);
for (i, p) in self.buckets[index].iter_mut().enumerate() {
if p.key == key {
let pair = self.buckets[index].remove(i);
self.size -= 1;
return Some(pair.val);
}
@@ -1075,7 +1074,7 @@ comments: true
/* 扩容哈希表 */
fn extend(&mut self) {
// 暂存原哈希表
let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);
let buckets_tmp = std::mem::take(&mut self.buckets);
// 初始化扩容后的新哈希表
self.capacity *= self.extend_ratio;
@@ -1109,30 +1108,27 @@ comments: true
}
let index = self.hash_func(key);
let bucket = &mut self.buckets[index];
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for pair in bucket {
for pair in self.buckets[index].iter_mut() {
if pair.key == key {
pair.val = val;
return;
}
}
let bucket = &mut self.buckets[index];
// 若无该 key ,则将键值对添加至尾部
let pair = Pair { key, val };
bucket.push(pair);
self.buckets[index].push(pair);
self.size += 1;
}
/* 查询操作 */
fn get(&self, key: i32) -> Option<&str> {
let index = self.hash_func(key);
let bucket = &self.buckets[index];
// 遍历桶,若找到 key ,则返回对应 val
for pair in bucket {
for pair in self.buckets[index].iter() {
if pair.key == key {
return Some(&pair.val);
}

View File

@@ -27,9 +27,7 @@ comments: true
"""计数排序"""
# 简单实现,无法用于排序对象
# 1. 统计数组最大元素 m
m = 0
for num in nums:
m = max(m, num)
m = max(nums)
# 2. 统计各数字的出现次数
# counter[num] 代表 num 的出现次数
counter = [0] * (m + 1)

View File

@@ -1679,7 +1679,7 @@ comments: true
}
}
self.que_size -= 1; // 更新队列长度
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
old_front.borrow().val
})
}
// 队尾出队操作
@@ -1695,7 +1695,7 @@ comments: true
}
}
self.que_size -= 1; // 更新队列长度
Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val
old_rear.borrow().val
})
}
}
@@ -1722,12 +1722,16 @@ comments: true
/* 返回数组用于打印 */
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
let mut nums = self.to_array(node.borrow().next.as_ref());
nums.insert(0, node.borrow().val);
return nums;
let mut res: Vec<T> = Vec::new();
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
if let Some(cur) = cur {
res.push(cur.borrow().val);
recur(cur.borrow().next.as_ref(), res);
}
}
return Vec::new();
recur(head, &mut res);
res
}
}
```

View File

@@ -1070,7 +1070,7 @@ comments: true
}
}
self.que_size -= 1;
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
old_front.borrow().val
})
}
@@ -1081,12 +1081,18 @@ comments: true
/* 将链表转化为 Array 并返回 */
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
let mut nums = self.to_array(node.borrow().next.as_ref());
nums.insert(0, node.borrow().val);
return nums;
let mut res: Vec<T> = Vec::new();
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
if let Some(cur) = cur {
res.push(cur.borrow().val);
recur(cur.borrow().next.as_ref(), res);
}
}
return Vec::new();
recur(head, &mut res);
res
}
}
```

View File

@@ -964,16 +964,10 @@ comments: true
/* 出栈 */
pub fn pop(&mut self) -> Option<T> {
self.stack_peek.take().map(|old_head| {
match old_head.borrow_mut().next.take() {
Some(new_head) => {
self.stack_peek = Some(new_head);
}
None => {
self.stack_peek = None;
}
}
self.stack_peek = old_head.borrow_mut().next.take();
self.stk_size -= 1;
Rc::try_unwrap(old_head).ok().unwrap().into_inner().val
old_head.borrow().val
})
}