mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
fix: check the rust codes and fix them (#653)
* fix: check the rust codes and fix it * Update binary_tree_bfs.rs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -72,6 +72,7 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化数组 */
|
||||
let arr = [0; 5];
|
||||
print!("数组 arr = ");
|
||||
print_util::print_array(&arr);
|
||||
|
||||
@@ -52,11 +52,12 @@
|
||||
_count += 1;
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for _ in &list {
|
||||
for _n in &list {
|
||||
_count += 1;
|
||||
} // 或者
|
||||
}
|
||||
// 或者
|
||||
// list.iter().for_each(|_| _count += 1);
|
||||
// let _count = list.iter().fold(0, |_count, _| _count + 1);
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ include!("../include/include.rs");
|
||||
|
||||
#[allow(dead_code)]
|
||||
struct MyList {
|
||||
nums: Vec<i32>,
|
||||
capacity: usize,
|
||||
size: usize,
|
||||
extend_ratio: usize,
|
||||
nums: Vec<i32>, // 数组(存储列表元素)
|
||||
capacity: usize, // 列表容量
|
||||
size: usize, // 列表长度(即当前元素数量)
|
||||
extend_ratio: usize, // 每次列表扩容的倍数
|
||||
}
|
||||
|
||||
#[allow(unused,unused_comparisons)]
|
||||
@@ -94,6 +94,7 @@ impl MyList {
|
||||
|
||||
/* 列表扩容 */
|
||||
pub fn extend_capacity(&mut self) {
|
||||
// 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组拷贝到新数组
|
||||
let new_capacity = self.capacity * self.extend_ratio;
|
||||
self.nums.resize(new_capacity, 0);
|
||||
// 更新列表容量
|
||||
@@ -102,6 +103,7 @@ impl MyList {
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
pub fn to_array(&mut self) -> Vec<i32> {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
let mut nums = Vec::new();
|
||||
for i in 0..self.size {
|
||||
nums.push(self.get(i));
|
||||
|
||||
Reference in New Issue
Block a user