mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-22 15:53:50 +08:00
build
This commit is contained in:
@ -517,7 +517,7 @@ comments: true
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
|
||||
let n1 = n0.borrow_mut().next.take();
|
||||
let n1 = n0.borrow_mut().next.take();
|
||||
P.borrow_mut().next = n1;
|
||||
n0.borrow_mut().next = Some(P);
|
||||
}
|
||||
@ -695,7 +695,9 @@ comments: true
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
|
||||
if n0.borrow().next.is_none() {return};
|
||||
if n0.borrow().next.is_none() {
|
||||
return;
|
||||
};
|
||||
// n0 -> P -> n1
|
||||
let P = n0.borrow_mut().next.take();
|
||||
if let Some(node) = P {
|
||||
@ -877,10 +879,13 @@ comments: true
|
||||
```rust title="linked_list.rs"
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
|
||||
if index <= 0 {return head};
|
||||
if let Some(node) = &head.borrow_mut().next {
|
||||
if index <= 0 {
|
||||
return head;
|
||||
};
|
||||
if let Some(node) = &head.borrow().next {
|
||||
return access(node.clone(), index - 1);
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
```
|
||||
@ -1076,7 +1081,9 @@ comments: true
|
||||
```rust title="linked_list.rs"
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
|
||||
if head.borrow().val == target {return index};
|
||||
if head.borrow().val == target {
|
||||
return index;
|
||||
};
|
||||
if let Some(node) = &head.borrow_mut().next {
|
||||
return find(node.clone(), target, index + 1);
|
||||
}
|
||||
|
@ -1815,16 +1815,16 @@ comments: true
|
||||
#[allow(dead_code)]
|
||||
struct MyList {
|
||||
arr: Vec<i32>, // 数组(存储列表元素)
|
||||
capacity: usize, // 列表容量
|
||||
size: usize, // 列表长度(当前元素数量)
|
||||
extend_ratio: usize, // 每次列表扩容的倍数
|
||||
capacity: usize, // 列表容量
|
||||
size: usize, // 列表长度(当前元素数量)
|
||||
extend_ratio: usize, // 每次列表扩容的倍数
|
||||
}
|
||||
|
||||
#[allow(unused,unused_comparisons)]
|
||||
#[allow(unused, unused_comparisons)]
|
||||
impl MyList {
|
||||
/* 构造方法 */
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let mut vec = Vec::new();
|
||||
let mut vec = Vec::new();
|
||||
vec.resize(capacity, 0);
|
||||
Self {
|
||||
arr: vec,
|
||||
@ -1847,13 +1847,17 @@ comments: true
|
||||
/* 访问元素 */
|
||||
pub fn get(&self, index: usize) -> i32 {
|
||||
// 索引如果越界,则抛出异常,下同
|
||||
if index >= self.size {panic!("索引越界")};
|
||||
if index >= self.size {
|
||||
panic!("索引越界")
|
||||
};
|
||||
return self.arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
pub fn set(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size {panic!("索引越界")};
|
||||
if index >= self.size {
|
||||
panic!("索引越界")
|
||||
};
|
||||
self.arr[index] = num;
|
||||
}
|
||||
|
||||
@ -1870,7 +1874,9 @@ comments: true
|
||||
|
||||
/* 在中间插入元素 */
|
||||
pub fn insert(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
if index >= self.size() {
|
||||
panic!("索引越界")
|
||||
};
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if self.size == self.capacity() {
|
||||
self.extend_capacity();
|
||||
@ -1886,7 +1892,9 @@ comments: true
|
||||
|
||||
/* 删除元素 */
|
||||
pub fn remove(&mut self, index: usize) -> i32 {
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
if index >= self.size() {
|
||||
panic!("索引越界")
|
||||
};
|
||||
let num = self.arr[index];
|
||||
// 将将索引 index 之后的元素都向前移动一位
|
||||
for j in (index..self.size - 1) {
|
||||
|
@ -35,11 +35,11 @@ comments: true
|
||||
# 元素内存地址 = 数组内存地址(首元素内存地址) + 元素长度 * 元素索引
|
||||
```
|
||||
|
||||
**Q**:删除节点后,是否需要把 `P.next` 设为 `None` 呢?
|
||||
**Q**:删除节点 `P` 后,是否需要把 `P.next` 设为 `None` 呢?
|
||||
|
||||
不修改 `P.next` 也可以。从该链表的角度看,从头节点遍历到尾节点已经不会遇到 `P` 了。这意味着节点 `P` 已经从链表中删除了,此时节点 `P` 指向哪里都不会对该链表产生影响。
|
||||
|
||||
从垃圾回收的角度看,对于 Java、Python、Go 等拥有自动垃圾回收机制的语言来说,节点 `P` 是否被回收取决于是否仍存在指向它的引用,而不是 `P.next` 的值。在 C 和 C++ 等语言中,我们需要手动释放节点内存。
|
||||
从数据结构与算法(做题)的角度看,不断开没有关系,只要保证程序的逻辑是正确的就行。从标准库的角度看,断开更加安全、逻辑更加清晰。如果不断开,假设被删除节点未被正常回收,那么它会影响后继节点的内存回收。
|
||||
|
||||
**Q**:在链表中插入和删除操作的时间复杂度是 $O(1)$ 。但是增删之前都需要 $O(n)$ 的时间查找元素,那为什么时间复杂度不是 $O(n)$ 呢?
|
||||
|
||||
@ -78,7 +78,3 @@ comments: true
|
||||
**Q**:初始化列表 `res = [0] * self.size()` 操作,会导致 `res` 的每个元素引用相同的地址吗?
|
||||
|
||||
不会。但二维数组会有这个问题,例如初始化二维列表 `res = [[0] * self.size()]` ,则多次引用了同一个列表 `[0]` 。
|
||||
|
||||
**Q**:在删除节点中,需要断开该节点与其后继节点之间的引用指向吗?
|
||||
|
||||
从数据结构与算法(做题)的角度看,不断开没有关系,只要保证程序的逻辑是正确的就行。从标准库的角度看,断开更加安全、逻辑更加清晰。如果不断开,假设被删除节点未被正常回收,那么它会影响后继节点的内存回收。
|
||||
|
Reference in New Issue
Block a user