diff --git a/codes/dart/chapter_array_and_linkedlist/array.dart b/codes/dart/chapter_array_and_linkedlist/array.dart index 2e4fc4b006..ffd835d45b 100644 --- a/codes/dart/chapter_array_and_linkedlist/array.dart +++ b/codes/dart/chapter_array_and_linkedlist/array.dart @@ -29,14 +29,14 @@ List extend(List nums, int enlarge) { return res; } -/* 在数组的索引 index 处插入元素 num */ -void insert(List nums, int num, int index) { +/* 在数组的索引 index 处插入元素 _num */ +void insert(List nums, int _num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 for (var i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } - // 将 num 赋给 index 处元素 - nums[index] = num; + // 将 _num 赋给 index 处元素 + nums[index] = _num; } /* 删除索引 index 处元素 */ @@ -55,12 +55,12 @@ void traverse(List nums) { count += nums[i]; } // 直接遍历数组元素 - for (int num in nums) { - count += num; + for (int _num in nums) { + count += _num; } // 通过 forEach 方法遍历数组 - nums.forEach((num) { - count += num; + nums.forEach((_num) { + count += _num; }); } diff --git a/codes/dart/chapter_array_and_linkedlist/list.dart b/codes/dart/chapter_array_and_linkedlist/list.dart index 784cf37f6d..050260a6ff 100644 --- a/codes/dart/chapter_array_and_linkedlist/list.dart +++ b/codes/dart/chapter_array_and_linkedlist/list.dart @@ -13,8 +13,8 @@ void main() { print('列表 nums = $nums'); /* 访问元素 */ - int num = nums[1]; - print('访问索引 1 处的元素,得到 num = $num'); + int _num = nums[1]; + print('访问索引 1 处的元素,得到 _num = $_num'); /* 更新元素 */ nums[1] = 0; diff --git a/codes/dart/chapter_array_and_linkedlist/my_list.dart b/codes/dart/chapter_array_and_linkedlist/my_list.dart index 374aee00ed..4d54309a8c 100644 --- a/codes/dart/chapter_array_and_linkedlist/my_list.dart +++ b/codes/dart/chapter_array_and_linkedlist/my_list.dart @@ -29,22 +29,22 @@ class MyList { } /* 更新元素 */ - void set(int index, int num) { + void set(int index, int _num) { if (index >= _size) throw RangeError('索引越界'); - _arr[index] = num; + _arr[index] = _num; } /* 尾部添加元素 */ - void add(int num) { + void add(int _num) { // 元素数量超出容量时,触发扩容机制 if (_size == _capacity) extendCapacity(); - _arr[_size] = num; + _arr[_size] = _num; // 更新元素数量 _size++; } /* 中间插入元素 */ - void insert(int index, int num) { + void insert(int index, int _num) { if (index >= _size) throw RangeError('索引越界'); // 元素数量超出容量时,触发扩容机制 if (_size == _capacity) extendCapacity(); @@ -52,7 +52,7 @@ class MyList { for (var j = _size - 1; j >= index; j--) { _arr[j + 1] = _arr[j]; } - _arr[index] = num; + _arr[index] = _num; // 更新元素数量 _size++; } @@ -60,7 +60,7 @@ class MyList { /* 删除元素 */ int remove(int index) { if (index >= _size) throw RangeError('索引越界'); - int num = _arr[index]; + int _num = _arr[index]; // 将索引 index 之后的元素都向前移动一位 for (var j = index; j < _size - 1; j++) { _arr[j] = _arr[j + 1]; @@ -68,7 +68,7 @@ class MyList { // 更新元素数量 _size--; // 返回被删除元素 - return num; + return _num; } /* 列表扩容 */ @@ -115,8 +115,8 @@ void main() { print('删除索引 3 处的元素,得到 nums = ${nums.toArray()}'); /* 访问元素 */ - int num = nums.get(1); - print('访问索引 1 处的元素,得到 num = $num'); + int _num = nums.get(1); + print('访问索引 1 处的元素,得到 _num = $_num'); /* 更新元素 */ nums.set(1, 0); diff --git a/codes/dart/chapter_computational_complexity/time_complexity.dart b/codes/dart/chapter_computational_complexity/time_complexity.dart index 2e6c62c495..18b17c6f97 100644 --- a/codes/dart/chapter_computational_complexity/time_complexity.dart +++ b/codes/dart/chapter_computational_complexity/time_complexity.dart @@ -29,7 +29,7 @@ int linear(int n) { int arrayTraversal(List nums) { int count = 0; // 循环次数与数组长度成正比 - for (var num in nums) { + for (var _num in nums) { count++; } return count; diff --git a/codes/dart/chapter_hashing/built_in_hash.dart b/codes/dart/chapter_hashing/built_in_hash.dart index 70fba328e6..4926e2812b 100644 --- a/codes/dart/chapter_hashing/built_in_hash.dart +++ b/codes/dart/chapter_hashing/built_in_hash.dart @@ -8,9 +8,9 @@ import '../chapter_stack_and_queue/linkedlist_deque.dart'; /* Driver Code */ void main() { - int num = 3; - int hashNum = num.hashCode; - print("整数 $num 的哈希值为 $hashNum"); + int _num = 3; + int hashNum = _num.hashCode; + print("整数 $_num 的哈希值为 $hashNum"); bool bol = true; int hashBol = bol.hashCode; diff --git a/codes/dart/chapter_sorting/bucket_sort.dart b/codes/dart/chapter_sorting/bucket_sort.dart index cf9f98b878..284c6ef119 100644 --- a/codes/dart/chapter_sorting/bucket_sort.dart +++ b/codes/dart/chapter_sorting/bucket_sort.dart @@ -11,11 +11,11 @@ void bucketSort(List nums) { List> buckets = List.generate(k, (index) => []); // 1. 将数组元素分配到各个桶中 - for (double num in nums) { - // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - int i = (num * k).toInt(); - // 将 num 添加进桶 bucket_idx - buckets[i].add(num); + for (double _num in nums) { + // 输入数据范围 [0, 1),使用 _num * k 映射到索引范围 [0, k-1] + int i = (_num * k).toInt(); + // 将 _num 添加进桶 bucket_idx + buckets[i].add(_num); } // 2. 对各个桶执行排序 for (List bucket in buckets) { @@ -24,8 +24,8 @@ void bucketSort(List nums) { // 3. 遍历桶合并结果 int i = 0; for (List bucket in buckets) { - for (double num in bucket) { - nums[i++] = num; + for (double _num in bucket) { + nums[i++] = _num; } } } diff --git a/codes/dart/chapter_sorting/counting_sort.dart b/codes/dart/chapter_sorting/counting_sort.dart index d7634ef834..92cf9b5467 100644 --- a/codes/dart/chapter_sorting/counting_sort.dart +++ b/codes/dart/chapter_sorting/counting_sort.dart @@ -10,20 +10,20 @@ import 'dart:math'; void countingSortNaive(List nums) { // 1. 统计数组最大元素 m int m = 0; - for (int num in nums) { - m = max(m, num); + for (int _num in nums) { + m = max(m, _num); } // 2. 统计各数字的出现次数 - // counter[num] 代表 num 的出现次数 + // counter[_num] 代表 _num 的出现次数 List counter = List.filled(m + 1, 0); - for (int num in nums) { - counter[num]++; + for (int _num in nums) { + counter[_num]++; } // 3. 遍历 counter ,将各元素填入原数组 nums int i = 0; - for (int num = 0; num < m + 1; num++) { - for (int j = 0; j < counter[num]; j++, i++) { - nums[i] = num; + for (int _num = 0; _num < m + 1; _num++) { + for (int j = 0; j < counter[_num]; j++, i++) { + nums[i] = _num; } } } @@ -33,17 +33,17 @@ void countingSortNaive(List nums) { void countingSort(List nums) { // 1. 统计数组最大元素 m int m = 0; - for (int num in nums) { - m = max(m, num); + for (int _num in nums) { + m = max(m, _num); } // 2. 统计各数字的出现次数 - // counter[num] 代表 num 的出现次数 + // counter[_num] 代表 _num 的出现次数 List counter = List.filled(m + 1, 0); - for (int num in nums) { - counter[num]++; + for (int _num in nums) { + counter[_num]++; } // 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引” - // 即 counter[num]-1 是 num 在 res 中最后一次出现的索引 + // 即 counter[_num]-1 是 _num 在 res 中最后一次出现的索引 for (int i = 0; i < m; i++) { counter[i + 1] += counter[i]; } @@ -52,9 +52,9 @@ void countingSort(List nums) { int n = nums.length; List res = List.filled(n, 0); for (int i = n - 1; i >= 0; i--) { - int num = nums[i]; - res[counter[num] - 1] = num; // 将 num 放置到对应索引处 - counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引 + int _num = nums[i]; + res[counter[_num] - 1] = _num; // 将 _num 放置到对应索引处 + counter[_num]--; // 令前缀和自减 1 ,得到下次放置 _num 的索引 } // 使用结果数组 res 覆盖原数组 nums nums.setAll(0, res); diff --git a/codes/dart/chapter_sorting/radix_sort.dart b/codes/dart/chapter_sorting/radix_sort.dart index 095dc90f94..3f19d15e18 100644 --- a/codes/dart/chapter_sorting/radix_sort.dart +++ b/codes/dart/chapter_sorting/radix_sort.dart @@ -4,10 +4,10 @@ * Author: what-is-me (whatisme@outlook.jp) */ -/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */ -int digit(int num, int exp) { +/* 获取元素 _num 的第 k 位,其中 exp = 10^(k-1) */ +int digit(int _num, int exp) { // 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算 - return (num ~/ exp) % 10; + return (_num ~/ exp) % 10; } /* 计数排序(根据 nums 第 k 位排序) */ @@ -41,7 +41,7 @@ void radixSort(List nums) { // 获取数组的最大元素,用于判断最大位数 // dart 中 int 的长度是 64 位的 int m = -1 << 63; - for (int num in nums) if (num > m) m = num; + for (int _num in nums) if (_num > m) m = _num; // 按照从低位到高位的顺序遍历 for (int exp = 1; exp <= m; exp *= 10) // 对数组元素的第 k 位执行计数排序 diff --git a/codes/dart/chapter_stack_and_queue/array_deque.dart b/codes/dart/chapter_stack_and_queue/array_deque.dart index 722867b008..34eba9c8d4 100644 --- a/codes/dart/chapter_stack_and_queue/array_deque.dart +++ b/codes/dart/chapter_stack_and_queue/array_deque.dart @@ -40,44 +40,44 @@ class ArrayDeque { } /* 队首入队 */ - void pushFirst(int num) { + void pushFirst(int _num) { if (_queSize == capacity()) { throw Exception("双向队列已满"); } // 队首指针向左移动一位 // 通过取余操作,实现 _front 越过数组头部后回到尾部 _front = index(_front - 1); - // 将 num 添加至队首 - _nums[_front] = num; + // 将 _num 添加至队首 + _nums[_front] = _num; _queSize++; } /* 队尾入队 */ - void pushLast(int num) { + void pushLast(int _num) { if (_queSize == capacity()) { throw Exception("双向队列已满"); } // 计算尾指针,指向队尾索引 + 1 int rear = index(_front + _queSize); - // 将 num 添加至队尾 - _nums[rear] = num; + // 将 _num 添加至队尾 + _nums[rear] = _num; _queSize++; } /* 队首出队 */ int popFirst() { - int num = peekFirst(); + int _num = peekFirst(); // 队首指针向右移动一位 _front = index(_front + 1); _queSize--; - return num; + return _num; } /* 队尾出队 */ int popLast() { - int num = peekLast(); + int _num = peekLast(); _queSize--; - return num; + return _num; } /* 访问队首元素 */ diff --git a/codes/dart/chapter_stack_and_queue/array_queue.dart b/codes/dart/chapter_stack_and_queue/array_queue.dart index 369af4c54e..8c25e75e81 100644 --- a/codes/dart/chapter_stack_and_queue/array_queue.dart +++ b/codes/dart/chapter_stack_and_queue/array_queue.dart @@ -31,25 +31,25 @@ class ArrayQueue { } /* 入队 */ - void push(int num) { + void push(int _num) { if (_queSize == capaCity()) { throw Exception("队列已满"); } // 计算尾指针,指向队尾索引 + 1 // 通过取余操作,实现 rear 越过数组尾部后回到头部 int rear = (_front + _queSize) % capaCity(); - // 将 num 添加至队尾 - _nums[rear] = num; + // 将 _num 添加至队尾 + _nums[rear] = _num; _queSize++; } /* 出队 */ int pop() { - int num = peek(); + int _num = peek(); // 队首指针向后移动一位,若越过尾部则返回到数组头部 _front = (_front + 1) % capaCity(); _queSize--; - return num; + return _num; } /* 访问队首元素 */ diff --git a/codes/dart/chapter_stack_and_queue/array_stack.dart b/codes/dart/chapter_stack_and_queue/array_stack.dart index 485d2359eb..bd6918f78e 100644 --- a/codes/dart/chapter_stack_and_queue/array_stack.dart +++ b/codes/dart/chapter_stack_and_queue/array_stack.dart @@ -22,8 +22,8 @@ class ArrayStack { } /* 入栈 */ - void push(int num) { - _stack.add(num); + void push(int _num) { + _stack.add(_num); } /* 出栈 */ diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart index 91df529c09..08d0dd8071 100644 --- a/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart +++ b/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -35,8 +35,8 @@ class LinkedListDeque { } /* 入队操作 */ - void push(int num, bool isFront) { - final ListNode node = ListNode(num); + void push(int _num, bool isFront) { + final ListNode node = ListNode(_num); if (isEmpty()) { // 若链表为空,则令 _front 和 _rear 都指向 node _front = _rear = node; @@ -57,13 +57,13 @@ class LinkedListDeque { } /* 队首入队 */ - void pushFirst(int num) { - push(num, true); + void pushFirst(int _num) { + push(_num, true); } /* 队尾入队 */ - void pushLast(int num) { - push(num, false); + void pushLast(int _num) { + push(_num, false); } /* 出队操作 */ diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart b/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart index cdd801ab76..567a640437 100644 --- a/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart +++ b/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart @@ -28,9 +28,9 @@ class LinkedListQueue { } /* 入队 */ - void push(int num) { - // 尾节点后添加 num - final node = ListNode(num); + void push(int _num) { + // 尾节点后添加 _num + final node = ListNode(_num); // 如果队列为空,则令头、尾节点都指向该节点 if (_front == null) { _front = node; @@ -45,11 +45,11 @@ class LinkedListQueue { /* 出队 */ int pop() { - final int num = peek(); + final int _num = peek(); // 删除头节点 _front = _front!.next; _queSize--; - return num; + return _num; } /* 访问队首元素 */ diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart b/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart index 44164db260..1efaa54636 100644 --- a/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart +++ b/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart @@ -26,8 +26,8 @@ class LinkedListStack { } /* 入栈 */ - void push(int num) { - final ListNode node = ListNode(num); + void push(int _num) { + final ListNode node = ListNode(_num); node.next = _stackPeek; _stackPeek = node; _stkSize++; @@ -35,10 +35,10 @@ class LinkedListStack { /* 出栈 */ int pop() { - final int num = peek(); + final int _num = peek(); _stackPeek = _stackPeek!.next; _stkSize--; - return num; + return _num; } /* 访问栈顶元素 */ diff --git a/codes/dart/chapter_tree/binary_search_tree.dart b/codes/dart/chapter_tree/binary_search_tree.dart index 901466584b..4deaef3d42 100644 --- a/codes/dart/chapter_tree/binary_search_tree.dart +++ b/codes/dart/chapter_tree/binary_search_tree.dart @@ -23,15 +23,15 @@ class BinarySearchTree { } /* 查找节点 */ - TreeNode? search(int num) { + TreeNode? search(int _num) { TreeNode? cur = _root; // 循环查找,越过叶节点后跳出 while (cur != null) { // 目标节点在 cur 的右子树中 - if (cur.val < num) + if (cur.val < _num) cur = cur.right; // 目标节点在 cur 的左子树中 - else if (cur.val > num) + else if (cur.val > _num) cur = cur.left; // 找到目标节点,跳出循环 else @@ -42,10 +42,10 @@ class BinarySearchTree { } /* 插入节点 */ - void insert(int num) { + void insert(int _num) { // 若树为空,则初始化根节点 if (_root == null) { - _root = TreeNode(num); + _root = TreeNode(_num); return; } TreeNode? cur = _root; @@ -53,25 +53,25 @@ class BinarySearchTree { // 循环查找,越过叶节点后跳出 while (cur != null) { // 找到重复节点,直接返回 - if (cur.val == num) return; + if (cur.val == _num) return; pre = cur; // 插入位置在 cur 的右子树中 - if (cur.val < num) + if (cur.val < _num) cur = cur.right; // 插入位置在 cur 的左子树中 else cur = cur.left; } // 插入节点 - TreeNode? node = TreeNode(num); - if (pre!.val < num) + TreeNode? node = TreeNode(_num); + if (pre!.val < _num) pre.right = node; else pre.left = node; } /* 删除节点 */ - void remove(int num) { + void remove(int _num) { // 若树为空,直接提前返回 if (_root == null) return; TreeNode? cur = _root; @@ -79,10 +79,10 @@ class BinarySearchTree { // 循环查找,越过叶节点后跳出 while (cur != null) { // 找到待删除节点,跳出循环 - if (cur.val == num) break; + if (cur.val == _num) break; pre = cur; // 待删除节点在 cur 的右子树中 - if (cur.val < num) + if (cur.val < _num) cur = cur.right; // 待删除节点在 cur 的左子树中 else @@ -125,8 +125,8 @@ void main() { BinarySearchTree bst = BinarySearchTree(); // 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树 List nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15]; - for (int num in nums) { - bst.insert(num); + for (int _num in nums) { + bst.insert(_num); } print("\n初始化的二叉树为\n"); printTree(bst.getRoot());