Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several bug fixes and improvements #945

Merged
merged 16 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<p align="center">
<a href="https://www.hello-algo.com/">
<img src="https://www.hello-algo.com/index.assets/hello_algo_header.png" width="450">
</a>
<img src="https://www.hello-algo.com/index.assets/hello_algo_header.png" width="450"></a>
</p>

<p align="center">
Expand All @@ -12,11 +11,9 @@

<p align="center">
<a href="https://www.hello-algo.com/">
<img src="https://www.hello-algo.com/index.assets/btn_read_online_dark.png" width="155">
</a>
<img src="https://www.hello-algo.com/index.assets/btn_read_online_dark.png" width="155"></a>
<a href="https://github.com/krahets/hello-algo/releases">
<img src="https://www.hello-algo.com/index.assets/btn_download_pdf_dark.png" width="155">
</a>
<img src="https://www.hello-algo.com/index.assets/btn_download_pdf_dark.png" width="155"></a>
</p>

<p align="center">
Expand Down Expand Up @@ -66,7 +63,7 @@

- [内容修正](https://www.hello-algo.com/chapter_appendix/contribution/):请您协助修正或在评论区指出语法错误、内容缺失、文字歧义、无效链接或代码 bug 等问题。
- [代码转译](https://github.com/krahets/hello-algo/issues/15):期待您贡献各种语言代码,已支持 Python、Java、C++、Go、JavaScript 等 12 门编程语言。
- [整书翻译](https://github.com/krahets/hello-algo/tree/en):诚邀您加入我们的中译英小组,成员主要来自计算机相关专业、英语专业和英文母语者。
- [Chinese-to-English](https://github.com/krahets/hello-algo/tree/en):诚邀您加入我们的翻译小组,成员主要来自计算机相关专业、英语专业和英文母语者。

欢迎您提出宝贵意见和建议,如有任何问题请提交 Issues 或微信联系 `krahets-jyd` 。

Expand Down
2 changes: 1 addition & 1 deletion codes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;; \
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_array_and_linkedlist/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void traverse(int *nums, int size) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
count++;
count += nums[i];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// 回退
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_array_and_linkedlist/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void traverse(int *nums, int size) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
count++;
count += nums[i];
}
}

Expand Down
11 changes: 5 additions & 6 deletions codes/cpp/chapter_array_and_linkedlist/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/* 拼接两个列表 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// 回退
Expand Down
6 changes: 3 additions & 3 deletions codes/csharp/chapter_array_and_linkedlist/array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
7 changes: 3 additions & 4 deletions codes/csharp/chapter_array_and_linkedlist/list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 拼接两个列表 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// 回退
Expand Down
30 changes: 15 additions & 15 deletions codes/dart/chapter_array_and_linkedlist/array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// 获取并返回随机元素
Expand All @@ -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);
// 将原数组中的所有元素复制到新数组
Expand All @@ -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];
Expand All @@ -40,32 +40,32 @@ 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];
}
}

/* 遍历数组元素 */
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;
}
Expand All @@ -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');

/* 随机访问 */
Expand All @@ -96,7 +96,7 @@ void main() {
remove(nums, 2);
print("删除索引 2 处的元素,得到 nums = $nums");

/* 遍历元素 */
/* 遍历数组 */
traverse(nums);

/* 查找元素 */
Expand Down
6 changes: 3 additions & 3 deletions codes/dart/chapter_array_and_linkedlist/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 拼接两个列表 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// 回退
Expand Down
2 changes: 1 addition & 1 deletion codes/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions codes/go/chapter_array_and_linkedlist/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
7 changes: 3 additions & 4 deletions codes/go/chapter_array_and_linkedlist/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/* 拼接两个列表 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions codes/java/chapter_array_and_linkedlist/array.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ static void traverse(int[] nums) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.length; i++) {
count++;
count += nums[i];
}
// 直接遍历数组
// 直接遍历数组元素
for (int num : nums) {
count++;
count += num;
}
}

Expand Down
12 changes: 5 additions & 7 deletions codes/java/chapter_array_and_linkedlist/list.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public static void main(String[] args) {
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);
Expand Down Expand Up @@ -47,13 +47,11 @@ public static void main(String[] args) {
/* 通过索引遍历列表 */
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;
}

/* 拼接两个列表 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void backtrack(List<Integer> choices, int state, int n, List<Integ
for (Integer choice : choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
break;
continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
Expand Down
6 changes: 3 additions & 3 deletions codes/javascript/chapter_array_and_linkedlist/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
7 changes: 3 additions & 4 deletions codes/javascript/chapter_array_and_linkedlist/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 拼接两个列表 */
Expand Down
Loading
Loading