Format C, C++, C#, Go, Java, Python, Rust code.

This commit is contained in:
krahets
2023-09-02 23:54:38 +08:00
parent b6ac6aa7d7
commit dd72335235
53 changed files with 152 additions and 154 deletions

View File

@ -241,7 +241,7 @@ void removeVertex(graphAdjList *t, unsigned int index) {
t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0
t->size--;
//释放被删除顶点的内存
// 释放内存
freeVertex(vet);
}

View File

@ -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
}

View File

@ -7,7 +7,7 @@
#include "../utils/common.h"
/* 哈希表默认数组大小 */
# define HASH_MAP_DEFAULT_SIZE 100
#define HASH_MAP_DEFAULT_SIZE 100
/* 键值对 int->string */
struct pair {
@ -154,7 +154,7 @@ void print(ArrayHashMap *d) {
int i;
mapSet set;
pairSet(d, &set);
pair *entries = (pair*) set.set;
pair *entries = (pair *)set.set;
for (i = 0; i < set.len; i++) {
printf("%d -> %s\n", entries[i].key, entries[i].val);
}
@ -164,7 +164,7 @@ void print(ArrayHashMap *d) {
/* Driver Code */
int main() {
/* 初始化哈希表 */
ArrayHashMap * map = newArrayHashMap();
ArrayHashMap *map = newArrayHashMap();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
@ -178,7 +178,7 @@ int main() {
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
const char * name = get(map, 15937);
const char *name = get(map, 15937);
printf("\n输入学号 15937 ,查询到姓名 %s\n", name);
/* 删除操作 */
@ -196,7 +196,7 @@ int main() {
mapSet set;
keySet(map, &set);
int *keys = (int*) set.set;
int *keys = (int *)set.set;
printf("\n单独遍历键 Key\n");
for (i = 0; i < set.len; i++) {
printf("%d\n", keys[i]);
@ -204,7 +204,7 @@ int main() {
free(set.set);
valueSet(map, &set);
char **vals = (char**) set.set;
char **vals = (char **)set.set;
printf("\n单独遍历键 Value\n");
for (i = 0; i < set.len; i++) {
printf("%s\n", vals[i]);

View File

@ -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 =";

View File

@ -31,7 +31,7 @@ int xorHash(string key) {
int hash = 0;
const int MODULUS = 1000000007;
for (unsigned char c : key) {
cout<<(int)c<<endl;
cout << (int)c << endl;
hash ^= (int)c;
}
return hash & MODULUS;

View File

@ -10,7 +10,7 @@ public class min_path_sum {
/* 最小路径和:暴力搜索 */
public int minPathSumDFS(int[][] grid, int i, int j) {
// 若为左上角单元格,则终止搜索
if (i == 0 && j == 0){
if (i == 0 && j == 0) {
return grid[0][0];
}
// 若行列索引越界,则返回 +∞ 代价

View File

@ -18,7 +18,7 @@ public class bucket_sort {
// 1. 将数组元素分配到各个桶中
foreach (float num in nums) {
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
int i = (int) (num * k);
int i = (int)(num * k);
// 将 num 添加进桶 i
buckets[i].Add(num);
}

View File

@ -36,7 +36,7 @@ public class TreeNode {
/* 将列表反序列化为二叉树:递归 */
private static TreeNode? ListToTreeDFS(List<int?> arr, int i) {
if (i < 0|| i >= arr.Count || !arr[i].HasValue) {
if (i < 0 || i >= arr.Count || !arr[i].HasValue) {
return null;
}
TreeNode root = new TreeNode(arr[i].Value);

View File

@ -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");

View File

@ -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) {

View File

@ -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)

View File

@ -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 = ")

View File

@ -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)}`);

View File

@ -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}")

View File

@ -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

View File

@ -4,7 +4,7 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

View File

@ -4,7 +4,7 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

View File

@ -4,7 +4,7 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

View File

@ -4,7 +4,7 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

View File

@ -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);

View File

@ -4,7 +4,7 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
use std::cmp;
use std::cmp;
/* 爬楼梯最小代价:动态规划 */
fn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {

View File

@ -4,12 +4,12 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
include!("../include/include.rs");
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
use list_node::ListNode;
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
use list_node::ListNode;
/* 哈希查找(数组) */
fn hashing_search_array<'a>(map: &'a HashMap<i32, usize>, target: i32) -> Option<&'a usize> {

View File

@ -4,11 +4,11 @@
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
include!("../include/include.rs");
use std::rc::Rc;
use std::cell::RefCell;
use list_node::ListNode;
use std::rc::Rc;
use std::cell::RefCell;
use list_node::ListNode;
/* 线性查找(数组) */
fn linear_search_array(nums: &[i32], target: i32) -> i32 {

View File

@ -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)}`);

View File

@ -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)$** 。