diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 739335938..a2d7627ef 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -239,7 +239,7 @@ comments: true ```dart title="array.dart" /* 随机访问元素 */ - int randomAccess(List nums) { + int randomAccess(List nums) { // 在区间 [0, nums.length) 中随机抽取一个数字 int randomIndex = Random().nextInt(nums.length); // 获取并返回随机元素 @@ -411,7 +411,7 @@ comments: true ```dart title="array.dart" /* 在数组的索引 index 处插入元素 num */ - void insert(List nums, int num, int index) { + void insert(List nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 for (var i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; @@ -573,7 +573,7 @@ comments: true ```dart title="array.dart" /* 删除索引 index 处元素 */ - void remove(List nums, int index) { + void remove(List nums, int index) { // 把索引 index 之后的所有元素向前移动一位 for (var i = index; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; @@ -772,19 +772,19 @@ comments: true ```dart title="array.dart" /* 遍历数组元素 */ - void traverse(List nums) { - var count = 0; + void traverse(List nums) { + int count = 0; // 通过索引遍历数组 for (var i = 0; i < nums.length; i++) { count += nums[i]; } // 直接遍历数组元素 - for (var num in nums) { - count += nums[i]; + for (int num in nums) { + count += num; } // 通过 forEach 方法遍历数组 - nums.forEach((element) { - count += element; + nums.forEach((num) { + count += num; }); } ``` @@ -954,7 +954,7 @@ comments: true ```dart title="array.dart" /* 在数组中查找指定元素 */ - int find(List nums, int target) { + int find(List nums, int target) { for (var i = 0; i < nums.length; i++) { if (nums[i] == target) return i; } @@ -1143,7 +1143,7 @@ comments: true ```dart title="array.dart" /* 扩展数组长度 */ - List extend(List nums, int enlarge) { + List extend(List nums, int enlarge) { // 初始化一个扩展长度后的数组 List res = List.filled(nums.length + enlarge, 0); // 将原数组中的所有元素复制到新数组 diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index cb8a4dd2d..1fba56838 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -158,7 +158,7 @@ comments: true /* 构造函数 */ ListNode *newListNode(int val) { - ListNode *node, *next; + ListNode *node; node = (ListNode *) malloc(sizeof(ListNode)); node->val = val; node->next = NULL; @@ -1285,7 +1285,7 @@ comments: true /* 构造函数 */ ListNode *newListNode(int val) { - ListNode *node, *next; + ListNode *node; node = (ListNode *) malloc(sizeof(ListNode)); node->val = val; node->next = NULL; diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 8e2d160cf..5f9b596d9 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -1817,13 +1817,13 @@ comments: true /* 访问元素 */ pub fn get(&self, index: usize) -> i32 { // 索引如果越界则抛出异常,下同 - if index < 0 || index >= self.size {panic!("索引越界")}; + if index >= self.size {panic!("索引越界")}; return self.arr[index]; } /* 更新元素 */ pub fn set(&mut self, index: usize, num: i32) { - if index < 0 || index >= self.size {panic!("索引越界")}; + if index >= self.size {panic!("索引越界")}; self.arr[index] = num; } @@ -1840,7 +1840,7 @@ comments: true /* 中间插入元素 */ pub fn insert(&mut self, index: usize, num: i32) { - if index < 0 || index >= self.size() {panic!("索引越界")}; + if index >= self.size() {panic!("索引越界")}; // 元素数量超出容量时,触发扩容机制 if self.size == self.capacity() { self.extend_capacity(); @@ -1856,7 +1856,7 @@ comments: true /* 删除元素 */ pub fn remove(&mut self, index: usize) -> i32 { - if index < 0 || index >= self.size() {panic!("索引越界")}; + if index >= self.size() {panic!("索引越界")}; let num = self.arr[index]; // 将索引 index 之后的元素都向前移动一位 for j in (index..self.size - 1) { diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 86e7c7d95..77257935c 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -288,7 +288,7 @@ comments: true stack.push(4); /* 访问栈顶元素 */ - let top = stack[stack.len() - 1]; + let top = stack.last().unwrap(); /* 元素出栈 */ let pop = stack.pop().unwrap(); diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index 534cfefcd..12b78337a 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -732,7 +732,7 @@ comments: true /* 获取索引为 i 节点的父节点的索引 */ parent(i) { - return (i - 1) / 2; + return Math.floor((i - 1) / 2); // 向下取整 } /* 层序遍历 */ @@ -818,7 +818,7 @@ comments: true /* 获取索引为 i 节点的父节点的索引 */ parent(i: number): number { - return (i - 1) / 2; + return Math.floor((i - 1) / 2); // 向下取整 } /* 层序遍历 */