fix(Rust): no need to clone in hash put fn (#1226)

* no need to clone in hash put fn

* fmt rust code

* make code more readable

* Change vec append to extend, more friendly to rookie rust dev

* drop comment
pull/1234/head
rongyi 7 months ago committed by GitHub
parent bd54594a90
commit f901a31bae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -102,17 +102,14 @@ impl HashMapChaining {
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回 // 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for pair in bucket { for pair in bucket {
if pair.key == key { if pair.key == key {
pair.val = val.clone(); pair.val = val;
return; return;
} }
} }
let bucket = &mut self.buckets[index]; let bucket = &mut self.buckets[index];
// 若无该 key ,则将键值对添加至尾部 // 若无该 key ,则将键值对添加至尾部
let pair = Pair { let pair = Pair { key, val };
key,
val: val.clone(),
};
bucket.push(pair); bucket.push(pair);
self.size += 1; self.size += 1;
} }

@ -14,7 +14,7 @@ use tree_node::{vec_to_tree, TreeNode};
fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> { fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {
// 初始化队列,加入根节点 // 初始化队列,加入根节点
let mut que = VecDeque::new(); let mut que = VecDeque::new();
que.push_back(Rc::clone(&root)); que.push_back(root.clone());
// 初始化一个列表,用于保存遍历序列 // 初始化一个列表,用于保存遍历序列
let mut vec = Vec::new(); let mut vec = Vec::new();
@ -22,10 +22,10 @@ fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {
// 队列出队 // 队列出队
vec.push(node.borrow().val); // 保存节点值 vec.push(node.borrow().val); // 保存节点值
if let Some(left) = node.borrow().left.as_ref() { if let Some(left) = node.borrow().left.as_ref() {
que.push_back(Rc::clone(left)); // 左子节点入队 que.push_back(left.clone()); // 左子节点入队
} }
if let Some(right) = node.borrow().right.as_ref() { if let Some(right) = node.borrow().right.as_ref() {
que.push_back(Rc::clone(right)); // 右子节点入队 que.push_back(right.clone()); // 右子节点入队
}; };
} }
vec vec

@ -17,8 +17,8 @@ fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
if let Some(node) = root { if let Some(node) = root {
// 访问优先级:根节点 -> 左子树 -> 右子树 // 访问优先级:根节点 -> 左子树 -> 右子树
result.push(node.borrow().val); result.push(node.borrow().val);
result.append(&mut pre_order(node.borrow().left.as_ref())); result.extend(pre_order(node.borrow().left.as_ref()));
result.append(&mut pre_order(node.borrow().right.as_ref())); result.extend(pre_order(node.borrow().right.as_ref()));
} }
result result
} }
@ -29,9 +29,9 @@ fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
if let Some(node) = root { if let Some(node) = root {
// 访问优先级:左子树 -> 根节点 -> 右子树 // 访问优先级:左子树 -> 根节点 -> 右子树
result.append(&mut in_order(node.borrow().left.as_ref())); result.extend(in_order(node.borrow().left.as_ref()));
result.push(node.borrow().val); result.push(node.borrow().val);
result.append(&mut in_order(node.borrow().right.as_ref())); result.extend(in_order(node.borrow().right.as_ref()));
} }
result result
} }
@ -42,8 +42,8 @@ fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
if let Some(node) = root { if let Some(node) = root {
// 访问优先级:左子树 -> 右子树 -> 根节点 // 访问优先级:左子树 -> 右子树 -> 根节点
result.append(&mut post_order(node.borrow().left.as_ref())); result.extend(post_order(node.borrow().left.as_ref()));
result.append(&mut post_order(node.borrow().right.as_ref())); result.extend(post_order(node.borrow().right.as_ref()));
result.push(node.borrow().val); result.push(node.borrow().val);
} }
result result

@ -25,7 +25,7 @@ impl TreeNode {
height: 0, height: 0,
parent: None, parent: None,
left: None, left: None,
right: None right: None,
})) }))
} }
} }
@ -34,7 +34,7 @@ impl TreeNode {
macro_rules! op_vec { macro_rules! op_vec {
( $( $x:expr ),* ) => { ( $( $x:expr ),* ) => {
vec![ vec![
$( Option::from($x).map(|x| x) ),* $( Option::from($x).map(|x| x) ),*
] ]
}; };
} }
@ -75,9 +75,10 @@ pub fn vec_to_tree(arr: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
fn tree_to_vec_dfs(root: Option<Rc<RefCell<TreeNode>>>, i: usize, res: &mut Vec<Option<i32>>) { fn tree_to_vec_dfs(root: Option<Rc<RefCell<TreeNode>>>, i: usize, res: &mut Vec<Option<i32>>) {
if root.is_none() { if root.is_none() {
return; return;
} }
let root = root.unwrap(); let root = root.unwrap();
while i >= res.len() { // i + 1 is the minimum valid size to access index i
while res.len() < i + 1 {
res.push(None); res.push(None);
} }
res[i] = Some(root.borrow().val); res[i] = Some(root.borrow().val);

Loading…
Cancel
Save