mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 07:46:06 +00: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:
+1
-1
@@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y wget
|
||||
# Install languages environment
|
||||
ARG LANGS
|
||||
RUN for LANG in $LANGS; do \
|
||||
case "$LANG" in \
|
||||
case $LANG in \
|
||||
python) \
|
||||
apt-get install -y python3.10 && \
|
||||
update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1 ;; \
|
||||
|
||||
@@ -55,7 +55,7 @@ void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < size; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ void backtrack(int *choices, int state, int n, int *res, int len) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n)
|
||||
break;
|
||||
continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res, len);
|
||||
// 回退
|
||||
|
||||
@@ -52,7 +52,7 @@ void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < size; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ int main() {
|
||||
printVector(nums);
|
||||
|
||||
/* 访问元素 */
|
||||
int x = nums[1];
|
||||
cout << "访问索引 1 处的元素,得到 x = " << x << endl;
|
||||
int num = nums[1];
|
||||
cout << "访问索引 1 处的元素,得到 num = " << num << endl;
|
||||
|
||||
/* 更新元素 */
|
||||
nums[1] = 0;
|
||||
@@ -49,13 +49,12 @@ int main() {
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int n : nums) {
|
||||
count++;
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -16,7 +16,7 @@ void backtrack(vector<int> &choices, int state, int n, vector<int> &res) {
|
||||
for (auto &choice : choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n)
|
||||
break;
|
||||
continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
@@ -50,11 +50,11 @@ public class array {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组
|
||||
// 直接遍历数组元素
|
||||
foreach (int num in nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,13 +46,12 @@ public class list {
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.Count; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
foreach (int n in nums) {
|
||||
count++;
|
||||
foreach (int x in nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -16,7 +16,7 @@ public class climbing_stairs_backtrack {
|
||||
foreach (int choice in choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n)
|
||||
break;
|
||||
continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
Backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
@@ -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);
|
||||
// 回退
|
||||
|
||||
@@ -6,7 +6,7 @@ services:
|
||||
args:
|
||||
# 设置需要安装的语言,使用空格隔开
|
||||
# Set the languages to be installed, separated by spaces
|
||||
LANGS: "python cpp csharp"
|
||||
LANGS: "python cpp java csharp"
|
||||
image: hello-algo-code
|
||||
container_name: hello-algo-code
|
||||
stdin_open: true
|
||||
|
||||
@@ -52,12 +52,17 @@ func traverse(nums []int) {
|
||||
count := 0
|
||||
// 通过索引遍历数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count++
|
||||
count += nums[i]
|
||||
}
|
||||
count = 0
|
||||
// 直接遍历数组
|
||||
for range nums {
|
||||
count++
|
||||
// 直接遍历数组元素
|
||||
for _, num := range nums {
|
||||
count += num
|
||||
}
|
||||
// 同时遍历数据索引和元素
|
||||
for i, num := range nums {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,13 +47,12 @@ func TestList(t *testing.T) {
|
||||
/* 通过索引遍历列表 */
|
||||
count := 0
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count++
|
||||
count += nums[i]
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0
|
||||
for range nums {
|
||||
count++
|
||||
for _, x := range nums {
|
||||
count += x
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -14,7 +14,7 @@ func backtrack(choices []int, state, n int, res []int) {
|
||||
for _, choice := range choices {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if state+choice > n {
|
||||
break
|
||||
continue
|
||||
}
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state+choice, n, res)
|
||||
|
||||
@@ -54,11 +54,11 @@ public class array {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组
|
||||
// 直接遍历数组元素
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ public class list {
|
||||
System.out.println("列表 nums = " + nums);
|
||||
|
||||
/* 访问元素 */
|
||||
int x = nums.get(1);
|
||||
System.out.println("访问索引 1 处的元素,得到 x = " + x);
|
||||
int num = nums.get(1);
|
||||
System.out.println("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
nums.set(1, 0);
|
||||
@@ -47,13 +47,11 @@ public class list {
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count++;
|
||||
count += nums.get(i);
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -18,7 +18,7 @@ public class climbing_stairs_backtrack {
|
||||
for (Integer choice : choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n)
|
||||
break;
|
||||
continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
@@ -50,11 +50,11 @@ function traverse(nums) {
|
||||
let count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组
|
||||
// 直接遍历数组元素
|
||||
for (const num of nums) {
|
||||
count += 1;
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,13 +39,12 @@ console.log(`删除索引 3 处的元素,得到 nums = ${nums}`);
|
||||
/* 通过索引遍历列表 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const n of nums) {
|
||||
count++;
|
||||
for (const x of nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -11,7 +11,7 @@ function backtrack(choices, state, n, res) {
|
||||
// 遍历所有选择
|
||||
for (const choice of choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n) break;
|
||||
if (state + choice > n) continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
@@ -50,13 +50,14 @@ def traverse(nums: list[int]):
|
||||
count = 0
|
||||
# 通过索引遍历数组
|
||||
for i in range(len(nums)):
|
||||
count += 1
|
||||
# 直接遍历数组
|
||||
count += nums[i]
|
||||
# 直接遍历数组元素
|
||||
for num in nums:
|
||||
count += 1
|
||||
count += num
|
||||
# 同时遍历数据索引和元素
|
||||
for i, num in enumerate(nums):
|
||||
count += 1
|
||||
count += nums[i]
|
||||
count += num
|
||||
|
||||
|
||||
def find(nums: list[int], target: int) -> int:
|
||||
|
||||
@@ -38,16 +38,13 @@ if __name__ == "__main__":
|
||||
nums.pop(3)
|
||||
print("\n删除索引 3 处的元素,得到 nums =", nums)
|
||||
|
||||
# 遍历列表
|
||||
tmp = []
|
||||
# 通过索引遍历列表
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
tmp.append(nums[i])
|
||||
print(f"\n通过索引遍历列表得到 tmp = {tmp}")
|
||||
|
||||
tmp.clear()
|
||||
count += nums[i]
|
||||
# 直接遍历列表元素
|
||||
for num in nums:
|
||||
tmp.append(num)
|
||||
print(f"\n直接遍历列表元素得到 tmp = {tmp}")
|
||||
count += num
|
||||
|
||||
# 拼接两个列表
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
|
||||
@@ -14,7 +14,7 @@ def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:
|
||||
for choice in choices:
|
||||
# 剪枝:不允许越过第 n 阶
|
||||
if state + choice > n:
|
||||
break
|
||||
continue
|
||||
# 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res)
|
||||
# 回退
|
||||
|
||||
@@ -35,7 +35,9 @@ def show_trunks(p: Trunk | None):
|
||||
print(p.str, end="")
|
||||
|
||||
|
||||
def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False):
|
||||
def print_tree(
|
||||
root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False
|
||||
):
|
||||
"""
|
||||
Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
|
||||
@@ -51,12 +51,12 @@ fn remove(nums: &mut Vec<i32>, index: usize) {
|
||||
fn traverse(nums: &[i32]) {
|
||||
let mut _count = 0;
|
||||
// 通过索引遍历数组
|
||||
for _ in 0..nums.len() {
|
||||
_count += 1;
|
||||
for i in 0..nums.len() {
|
||||
_count += nums[i];
|
||||
}
|
||||
// 直接遍历数组
|
||||
for _ in nums {
|
||||
_count += 1;
|
||||
// 直接遍历数组元素
|
||||
for num in nums {
|
||||
_count += num;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,26 +8,26 @@ include!("../include/include.rs");
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// 初始化列表
|
||||
// 初始化列表
|
||||
let mut nums: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("列表 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 访问元素
|
||||
// 访问元素
|
||||
let num = nums[1];
|
||||
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
// 更新元素
|
||||
// 更新元素
|
||||
nums[1] = 0;
|
||||
print!("将索引 1 处的元素更新为 0 ,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 清空列表
|
||||
// 清空列表
|
||||
nums.clear();
|
||||
print!("\n清空列表后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 尾部添加元素
|
||||
// 尾部添加元素
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
@@ -36,39 +36,35 @@ fn main() {
|
||||
print!("\n添加元素后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 中间插入元素
|
||||
// 中间插入元素
|
||||
nums.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 删除元素
|
||||
// 删除元素
|
||||
nums.remove(3);
|
||||
print!("\n删除索引 3 处的元素,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 通过索引遍历列表
|
||||
// 通过索引遍历列表
|
||||
let mut _count = 0;
|
||||
for _ in 0..nums.len() {
|
||||
_count += 1;
|
||||
for i in 0..nums.len() {
|
||||
_count += nums[i];
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for _n in &nums {
|
||||
_count += 1;
|
||||
}
|
||||
// 或者
|
||||
// nums.iter().for_each(|_| _count += 1);
|
||||
// let _count = nums.iter().fold(0, |_count, _| _count + 1);
|
||||
for x in &nums {
|
||||
_count += x;
|
||||
}
|
||||
|
||||
// 拼接两个列表
|
||||
// 拼接两个列表
|
||||
let mut nums1 = vec![ 6, 8, 7, 10, 9 ];
|
||||
nums.append(&mut nums1); // append(移动) 之后 nums1 为空!
|
||||
// nums.extend(&nums1); // extend(借用) nums1 能继续使用
|
||||
print!("\n将列表 nums1 拼接到 nums 之后,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 排序列表
|
||||
// 排序列表
|
||||
nums.sort();
|
||||
print!("\n排序列表后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
@@ -11,7 +11,7 @@ fn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {
|
||||
// 遍历所有选择
|
||||
for &choice in choices {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if state + choice > n { break; }
|
||||
if state + choice > n { continue; }
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
@@ -48,12 +48,12 @@ func remove(nums: inout [Int], index: Int) {
|
||||
func traverse(nums: [Int]) {
|
||||
var count = 0
|
||||
// 通过索引遍历数组
|
||||
for _ in nums.indices {
|
||||
count += 1
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
// 直接遍历数组
|
||||
for _ in nums {
|
||||
count += 1
|
||||
// 直接遍历数组元素
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,14 +42,13 @@ enum List {
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
var count = 0
|
||||
for _ in nums.indices {
|
||||
count += 1
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0
|
||||
for _ in nums {
|
||||
count += 1
|
||||
for x in nums {
|
||||
count += x
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -14,7 +14,7 @@ func backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {
|
||||
for choice in choices {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if state + choice > n {
|
||||
break
|
||||
continue
|
||||
}
|
||||
backtrack(choices: choices, state: state + choice, n: n, res: &res)
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ function traverse(nums: number[]): void {
|
||||
let count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组
|
||||
// 直接遍历数组元素
|
||||
for (const num of nums) {
|
||||
count += 1;
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,13 +39,12 @@ console.log(`删除索引 3 处的元素,得到 nums = ${nums}`);
|
||||
/* 通过索引遍历列表 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const n of nums) {
|
||||
count++;
|
||||
for (const x of nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
|
||||
@@ -16,7 +16,7 @@ function backtrack(
|
||||
// 遍历所有选择
|
||||
for (const choice of choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n) break;
|
||||
if (state + choice > n) continue;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
|
||||
@@ -51,12 +51,12 @@ pub fn traverse(nums: []i32) void {
|
||||
// 通过索引遍历数组
|
||||
var i: i32 = 0;
|
||||
while (i < nums.len) : (i += 1) {
|
||||
count += 1;
|
||||
count += nums[i];
|
||||
}
|
||||
count = 0;
|
||||
// 直接遍历数组
|
||||
for (nums) |_| {
|
||||
count += 1;
|
||||
// 直接遍历数组元素
|
||||
for (nums) |num| {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,13 +52,12 @@ pub fn main() !void {
|
||||
var count: i32 = 0;
|
||||
var i: i32 = 0;
|
||||
while (i < nums.items.len) : (i += 1) {
|
||||
count += 1;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
count = 0;
|
||||
for (nums.items) |_| {
|
||||
count += 1;
|
||||
for (nums.items) |x| {
|
||||
count += x;
|
||||
}
|
||||
|
||||
// 拼接两个列表
|
||||
|
||||
@@ -14,7 +14,7 @@ fn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {
|
||||
for (choices) |choice| {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n) {
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
|
||||
Reference in New Issue
Block a user