mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Format C, C++, C#, Go, Java, Python, Rust code.
This commit is contained in:
@ -241,7 +241,7 @@ void removeVertex(graphAdjList *t, unsigned int index) {
|
||||
t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0
|
||||
t->size--;
|
||||
|
||||
//释放被删除顶点的内存
|
||||
// 释放内存
|
||||
freeVertex(vet);
|
||||
}
|
||||
|
||||
|
||||
@ -49,8 +49,6 @@ void addVertex(graphAdjMat *t, int val) {
|
||||
// 如果实际使用不大于预设空间,则直接初始化新空间
|
||||
if (t->size < t->capacity) {
|
||||
t->vertices[t->size] = val; // 初始化新顶点值
|
||||
|
||||
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
t->adjMat[i][t->size] = 0; // 邻接矩新列阵置0
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ void dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
void hanota(vector<int> &A, vector<int> &B, vector<int> &C) {
|
||||
void solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {
|
||||
int n = A.size();
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
@ -52,7 +52,7 @@ int main() {
|
||||
cout << "C =";
|
||||
printVector(C);
|
||||
|
||||
hanota(A, B, C);
|
||||
solveHanota(A, B, C);
|
||||
|
||||
cout << "圆盘移动完成后:\n";
|
||||
cout << "A =";
|
||||
|
||||
@ -28,7 +28,7 @@ void dfs(int i, List<int> src, List<int> buf, List<int> tar) {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
void hanota(List<int> A, List<int> B, List<int> C) {
|
||||
void solveHanota(List<int> A, List<int> B, List<int> C) {
|
||||
int n = A.length;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
@ -45,7 +45,7 @@ void main() {
|
||||
print("B = $B");
|
||||
print("C = $C");
|
||||
|
||||
hanota(A, B, C);
|
||||
solveHanota(A, B, C);
|
||||
|
||||
print("圆盘移动完成后:");
|
||||
print("A = $A");
|
||||
|
||||
@ -6,8 +6,9 @@ package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestBuildTree(t *testing.T) {
|
||||
|
||||
@ -32,7 +32,7 @@ func dfsHanota(i int, src, buf, tar *list.List) {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
func hanota(A, B, C *list.List) {
|
||||
func solveHanota(A, B, C *list.List) {
|
||||
n := A.Len()
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfsHanota(n, A, B, C)
|
||||
|
||||
@ -7,8 +7,9 @@ package chapter_divide_and_conquer
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestHanota(t *testing.T) {
|
||||
@ -27,7 +28,7 @@ func TestHanota(t *testing.T) {
|
||||
fmt.Print("C = ")
|
||||
PrintList(C)
|
||||
|
||||
hanota(A, B, C)
|
||||
solveHanota(A, B, C)
|
||||
|
||||
fmt.Println("圆盘移动完成后:")
|
||||
fmt.Print("A = ")
|
||||
|
||||
@ -28,7 +28,7 @@ function dfs(i, src, buf, tar) {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
function hanota(A, B, C) {
|
||||
function solveHanota(A, B, C) {
|
||||
const n = A.length;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
@ -44,7 +44,7 @@ console.log(`A = ${JSON.stringify(A)}`);
|
||||
console.log(`B = ${JSON.stringify(B)}`);
|
||||
console.log(`C = ${JSON.stringify(C)}`);
|
||||
|
||||
hanota(A, B, C);
|
||||
solveHanota(A, B, C);
|
||||
|
||||
console.log('圆盘移动完成后:');
|
||||
console.log(`A = ${JSON.stringify(A)}`);
|
||||
|
||||
@ -27,7 +27,7 @@ def dfs(i: int, src: list[int], buf: list[int], tar: list[int]):
|
||||
dfs(i - 1, buf, src, tar)
|
||||
|
||||
|
||||
def hanota(A: list[int], B: list[int], C: list[int]):
|
||||
def solve_hanota(A: list[int], B: list[int], C: list[int]):
|
||||
"""求解汉诺塔"""
|
||||
n = len(A)
|
||||
# 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
@ -45,7 +45,7 @@ if __name__ == "__main__":
|
||||
print(f"B = {B}")
|
||||
print(f"C = {C}")
|
||||
|
||||
hanota(A, B, C)
|
||||
solve_hanota(A, B, C)
|
||||
|
||||
print("圆盘移动完成后:")
|
||||
print(f"A = {A}")
|
||||
|
||||
@ -35,9 +35,7 @@ def show_trunks(p: Trunk | None):
|
||||
print(p.str, end="")
|
||||
|
||||
|
||||
def print_tree(
|
||||
root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False
|
||||
):
|
||||
def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False):
|
||||
"""
|
||||
Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
|
||||
@ -30,7 +30,7 @@ fn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
fn hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {
|
||||
fn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {
|
||||
let n = A.len() as i32;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
@ -46,7 +46,7 @@ pub fn main() {
|
||||
println!("B = {:?}", B);
|
||||
println!("C = {:?}", C);
|
||||
|
||||
hanota(&mut A, &mut B, &mut C);
|
||||
solve_hanota(&mut A, &mut B, &mut C);
|
||||
|
||||
println!("圆盘移动完成后:");
|
||||
println!("A = {:?}", A);
|
||||
|
||||
@ -28,7 +28,7 @@ function dfs(i: number, src: number[], buf: number[], tar: number[]): void {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
function hanota(A: number[], B: number[], C: number[]): void {
|
||||
function solveHanota(A: number[], B: number[], C: number[]): void {
|
||||
const n = A.length;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
@ -44,7 +44,7 @@ console.log(`A = ${JSON.stringify(A)}`);
|
||||
console.log(`B = ${JSON.stringify(B)}`);
|
||||
console.log(`C = ${JSON.stringify(C)}`);
|
||||
|
||||
hanota(A, B, C);
|
||||
solveHanota(A, B, C);
|
||||
|
||||
console.log('圆盘移动完成后:');
|
||||
console.log(`A = ${JSON.stringify(A)}`);
|
||||
|
||||
@ -99,7 +99,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -109,7 +109,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solve_hanota}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -119,7 +119,7 @@
|
||||
|
||||
[class]{}-[func]{dfsHanota}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
@ -129,7 +129,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
@ -139,7 +139,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@ -149,7 +149,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -169,7 +169,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -179,7 +179,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
@ -189,7 +189,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@ -199,7 +199,7 @@
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{hanota}
|
||||
[class]{}-[func]{solve_hanota}
|
||||
```
|
||||
|
||||
如下图所示,汉诺塔问题形成一个高度为 $n$ 的递归树,每个节点代表一个子问题、对应一个开启的 `dfs()` 函数,**因此时间复杂度为 $O(2^n)$ ,空间复杂度为 $O(n)$** 。
|
||||
|
||||
Reference in New Issue
Block a user