mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-11 19:17:05 +08:00
Several bug fixes and improvements (#945)
* Update Dockerfile for code debugging. * Format Python code using Black. * Improve dark theme by defining html classes for the figures, animations and cover images. * Fix several glossary translation. * Update a code comment. * Fix climbing_stairs_backtrack: the pruning should not require the sorted choices list. * Update the code of array and list traversal. * Fix a rendering issue of README.md * Update code of list traversal. * Fix array_definition.png * Update README.md * Fix max_capacity_moving_short_board.png * Fix array.dart * Fix array.dart * Fix array.dart * Fix array.dart
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
import 'dart:math';
|
||||
|
||||
/* 随机访问元素 */
|
||||
int randomAccess(List nums) {
|
||||
int randomAccess(List<int> nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
int randomIndex = Random().nextInt(nums.length);
|
||||
// 获取并返回随机元素
|
||||
@@ -18,7 +18,7 @@ int randomAccess(List nums) {
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
List extend(List nums, int enlarge) {
|
||||
List<int> extend(List<int> nums, int enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
List<int> res = List.filled(nums.length + enlarge, 0);
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
@@ -30,7 +30,7 @@ List extend(List nums, int enlarge) {
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(List nums, int num, int index) {
|
||||
void insert(List<int> nums, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (var i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
@@ -40,7 +40,7 @@ void insert(List nums, int num, int index) {
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
void remove(List nums, int index) {
|
||||
void remove(List<int> nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (var i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
@@ -48,24 +48,24 @@ void remove(List nums, int index) {
|
||||
}
|
||||
|
||||
/* 遍历数组元素 */
|
||||
void traverse(List nums) {
|
||||
var count = 0;
|
||||
void traverse(List<int> nums) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组
|
||||
for (var num in nums) {
|
||||
count++;
|
||||
// 直接遍历数组元素
|
||||
for (int num in nums) {
|
||||
count += num;
|
||||
}
|
||||
// 通过 forEach 方法遍历数组
|
||||
nums.forEach((element) {
|
||||
count++;
|
||||
nums.forEach((num) {
|
||||
count += num;
|
||||
});
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
int find(List nums, int target) {
|
||||
int find(List<int> nums, int target) {
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target) return i;
|
||||
}
|
||||
@@ -77,7 +77,7 @@ void main() {
|
||||
/* 初始化数组 */
|
||||
var arr = List.filled(5, 0);
|
||||
print('数组 arr = $arr');
|
||||
List nums = [1, 3, 2, 5, 4];
|
||||
List<int> nums = [1, 3, 2, 5, 4];
|
||||
print('数组 nums = $nums');
|
||||
|
||||
/* 随机访问 */
|
||||
@@ -96,7 +96,7 @@ void main() {
|
||||
remove(nums, 2);
|
||||
print("删除索引 2 处的元素,得到 nums = $nums");
|
||||
|
||||
/* 遍历元素 */
|
||||
/* 遍历数组 */
|
||||
traverse(nums);
|
||||
|
||||
/* 查找元素 */
|
||||
|
||||
@@ -43,12 +43,12 @@ void main() {
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (var n in nums) {
|
||||
count++;
|
||||
for (var x in nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -13,7 +13,7 @@ void backtrack(List<int> choices, int state, int n, List<int> res) {
|
||||
// 遍历所有选择
|
||||
for (int choice in choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n) break;
|
||||
if (state + choice > n) continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
Reference in New Issue
Block a user