Skip to content

Commit

Permalink
fix(Dart): Avoid using num as a variable name (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvenusleo authored Nov 15, 2023
1 parent fcbaf10 commit 82f0063
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 98 deletions.
16 changes: 8 additions & 8 deletions codes/dart/chapter_array_and_linkedlist/array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ List<int> extend(List<int> nums, int enlarge) {
return res;
}

/* 在数组的索引 index 处插入元素 num */
void insert(List<int> nums, int num, int index) {
/* 在数组的索引 index 处插入元素 _num */
void insert(List<int> 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 处元素 */
Expand All @@ -55,12 +55,12 @@ void traverse(List<int> 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;
});
}

Expand Down
4 changes: 2 additions & 2 deletions codes/dart/chapter_array_and_linkedlist/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions codes/dart/chapter_array_and_linkedlist/my_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,46 @@ 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();
// 将索引 index 以及之后的元素都向后移动一位
for (var j = _size - 1; j >= index; j--) {
_arr[j + 1] = _arr[j];
}
_arr[index] = num;
_arr[index] = _num;
// 更新元素数量
_size++;
}

/* 删除元素 */
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];
}
// 更新元素数量
_size--;
// 返回被删除元素
return num;
return _num;
}

/* 列表扩容 */
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int linear(int n) {
int arrayTraversal(List<int> nums) {
int count = 0;
// 循环次数与数组长度成正比
for (var num in nums) {
for (var _num in nums) {
count++;
}
return count;
Expand Down
6 changes: 3 additions & 3 deletions codes/dart/chapter_hashing/built_in_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions codes/dart/chapter_sorting/bucket_sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ void bucketSort(List<double> nums) {
List<List<double>> 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<double> bucket in buckets) {
Expand All @@ -24,8 +24,8 @@ void bucketSort(List<double> nums) {
// 3. 遍历桶合并结果
int i = 0;
for (List<double> bucket in buckets) {
for (double num in bucket) {
nums[i++] = num;
for (double _num in bucket) {
nums[i++] = _num;
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions codes/dart/chapter_sorting/counting_sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import 'dart:math';
void countingSortNaive(List<int> 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<int> 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;
}
}
}
Expand All @@ -33,17 +33,17 @@ void countingSortNaive(List<int> nums) {
void countingSort(List<int> 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<int> 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];
}
Expand All @@ -52,9 +52,9 @@ void countingSort(List<int> nums) {
int n = nums.length;
List<int> 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);
Expand Down
8 changes: 4 additions & 4 deletions codes/dart/chapter_sorting/radix_sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Author: what-is-me ([email protected])
*/

/* 获取元素 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 位排序) */
Expand Down Expand Up @@ -41,7 +41,7 @@ void radixSort(List<int> 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 位执行计数排序
Expand Down
20 changes: 10 additions & 10 deletions codes/dart/chapter_stack_and_queue/array_deque.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 访问队首元素 */
Expand Down
10 changes: 5 additions & 5 deletions codes/dart/chapter_stack_and_queue/array_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 访问队首元素 */
Expand Down
4 changes: 2 additions & 2 deletions codes/dart/chapter_stack_and_queue/array_stack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ArrayStack {
}

/* 入栈 */
void push(int num) {
_stack.add(num);
void push(int _num) {
_stack.add(_num);
}

/* 出栈 */
Expand Down
12 changes: 6 additions & 6 deletions codes/dart/chapter_stack_and_queue/linkedlist_deque.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/* 出队操作 */
Expand Down
Loading

0 comments on commit 82f0063

Please sign in to comment.