Skip to content

Commit

Permalink
cargo fmt rust code (krahets#1131)
Browse files Browse the repository at this point in the history
* cargo fmt code

* Add empty line to seperate unrelated comments

* Fix review

* Update bubble_sort.rs

* Update merge_sort.rs

---------

Co-authored-by: Yudong Jin <[email protected]>
  • Loading branch information
rongyi and krahets authored Mar 15, 2024
1 parent 54ceef3 commit 7b10943
Show file tree
Hide file tree
Showing 70 changed files with 1,021 additions and 836 deletions.
2 changes: 1 addition & 1 deletion codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() {
// 在 Rust 中,指定长度时([i32; 5])为数组
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组(Array)也是rust一般情况下使用动态数组的类型
let nums = vec![ 1, 3, 2, 5, 4 ];
let nums = vec![1, 3, 2, 5, 4];
print!("\n数组 nums = ");
print_util::print_array(&nums);

Expand Down
20 changes: 13 additions & 7 deletions codes/rust/chapter_array_and_linkedlist/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@

include!("../include/include.rs");

use std::rc::Rc;
use std::cell::RefCell;
use list_node::ListNode;
use std::cell::RefCell;
use std::rc::Rc;

/* 在链表的节点 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);
}

/* 删除链表的节点 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 {
Expand All @@ -32,7 +34,9 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {

/* 访问链表中索引为 index 的节点 */
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
if index <= 0 {return head};
if index <= 0 {
return head;
};
if let Some(node) = &head.borrow_mut().next {
return access(node.clone(), index - 1);
}
Expand All @@ -41,7 +45,9 @@ pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListN

/* 在链表中查找值为 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);
}
Expand All @@ -51,7 +57,7 @@ pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32)
/* Driver Code */
fn main() {
/* 初始化链表 */
// 初始化各个节点
// 初始化各个节点
let n0 = ListNode::new(1);
let n1 = ListNode::new(3);
let n2 = ListNode::new(2);
Expand Down
9 changes: 5 additions & 4 deletions codes/rust/chapter_array_and_linkedlist/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include!("../include/include.rs");
/* Driver Code */
fn main() {
// 初始化列表
let mut nums: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
print!("列表 nums = ");
print_util::print_array(&nums);

Expand Down Expand Up @@ -58,9 +58,10 @@ fn main() {
}

// 拼接两个列表
let mut nums1 = vec![ 6, 8, 7, 10, 9 ];
nums.append(&mut nums1); // append(移动) 之后 nums1 为空!
// nums.extend(&nums1); // extend(借用) nums1 能继续使用
let mut nums1 = vec![6, 8, 7, 10, 9];
nums.append(&mut nums1); // append(移动) 之后 nums1 为空!

// nums.extend(&nums1); // extend(借用) nums1 能继续使用
print!("\n将列表 nums1 拼接到 nums 之后,得到 nums = ");
print_util::print_array(&nums);

Expand Down
26 changes: 17 additions & 9 deletions codes/rust/chapter_array_and_linkedlist/my_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ include!("../include/include.rs");
#[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,
Expand All @@ -42,13 +42,17 @@ impl MyList {
/* 访问元素 */
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;
}

Expand All @@ -65,7 +69,9 @@ impl MyList {

/* 在中间插入元素 */
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();
Expand All @@ -81,7 +87,9 @@ impl MyList {

/* 删除元素 */
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) {
Expand Down
21 changes: 18 additions & 3 deletions codes/rust/chapter_backtracking/n_queens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
*/

/* 回溯算法:n 皇后 */
fn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {
fn backtrack(
row: usize,
n: usize,
state: &mut Vec<Vec<String>>,
res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool],
diags1: &mut [bool],
diags2: &mut [bool],
) {
// 当放置完所有行时,记录解
if row == n {
let mut copy_state: Vec<Vec<String>> = Vec::new();
Expand Down Expand Up @@ -51,7 +58,15 @@ fn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {
let mut diags2 = vec![false; 2 * n - 1]; // 记录次对角线上是否有皇后
let mut res: Vec<Vec<Vec<String>>> = Vec::new();

backtrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);
backtrack(
0,
n,
&mut state,
&mut res,
&mut cols,
&mut diags1,
&mut diags2,
);

res
}
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_backtracking/permutations_i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {

/* Driver Code */
pub fn main() {
let mut nums = [ 1, 2, 3 ];
let mut nums = [1, 2, 3];

let res = permutations_i(&mut nums);

Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_backtracking/permutations_ii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {

/* Driver Code */
pub fn main() {
let mut nums = [ 1, 2, 2 ];
let mut nums = [1, 2, 2];

let res = permutations_ii(&mut nums);

Expand Down
104 changes: 54 additions & 50 deletions codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
/*
* File: preorder_traversal_ii_compact.rs
* Created Time: 2023-07-15
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序遍历:例题二 */
fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
if root.is_none() {
return;
}
if let Some(node) = root {
// 尝试
path.push(node.clone());
if node.borrow().val == 7 {
// 记录解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
// 回退
path.remove(path.len() - 1);
}
}

/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("初始化二叉树");
print_util::print_tree(root.as_ref().unwrap());

// 前序遍历
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);

println!("\n输出所有根节点到节点 7 的路径");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}
/*
* File: preorder_traversal_ii_compact.rs
* Created Time: 2023-07-15
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序遍历:例题二 */
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<Rc<RefCell<TreeNode>>>,
) {
if root.is_none() {
return;
}
if let Some(node) = root {
// 尝试
path.push(node.clone());
if node.borrow().val == 7 {
// 记录解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
// 回退
path.remove(path.len() - 1);
}
}

/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("初始化二叉树");
print_util::print_tree(root.as_ref().unwrap());

// 前序遍历
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);

println!("\n输出所有根节点到节点 7 的路径");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}
Loading

0 comments on commit 7b10943

Please sign in to comment.