Skip to content

Commit

Permalink
Format C, C++, C#, Go, Java, Python, Rust code.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Sep 2, 2023
1 parent b6ac6aa commit dd72335
Show file tree
Hide file tree
Showing 53 changed files with 152 additions and 154 deletions.
2 changes: 1 addition & 1 deletion codes/c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
!*/
*.dSYM/

build/
build/
6 changes: 3 additions & 3 deletions codes/c/chapter_backtracking/subset_sum_ii.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ int comp(const void *a, const void *b) {

/* 求解子集和 II */
vector *subsetSumII(vector *nums, int target) {
vector *state = newVector(); // 状态(子集)
vector *state = newVector(); // 状态(子集)
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序
int start = 0; // 子集和
vector *res = newVector(); // 结果列表(子集列表)
int start = 0; // 子集和
vector *res = newVector(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_computational_complexity/time_complexity.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int bubbleSort(int *nums, int n) {
int count = 0; // 计数器
// 外循环:未排序区间为 [0, i]
for (int i = n - 1; i > 0; i--) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_graph/graph_adjacency_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void removeVertex(graphAdjList *t, unsigned int index) {
Node *temp = vet->linked->head->next;
while (temp != 0) {
removeLink(temp->val->linked, vet); // 删除与该顶点有关的边
temp = temp->next;
temp = temp->next;
}

// 将顶点前移
Expand All @@ -241,7 +241,7 @@ void removeVertex(graphAdjList *t, unsigned int index) {
t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0
t->size--;

//释放被删除顶点的内存
// 释放内存
freeVertex(vet);
}

Expand Down Expand Up @@ -273,5 +273,5 @@ graphAdjList *newGraphAdjList(unsigned int verticesCapacity) {
newGraph->size = 0; // 初始化顶点数量
newGraph->capacity = verticesCapacity; // 初始化顶点容量
// 返回图指针
return newGraph;
return newGraph;
}
16 changes: 7 additions & 9 deletions codes/c/chapter_graph/graph_adjacency_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -90,7 +88,7 @@ void addVertex(graphAdjMat *t, int val) {
free(t->adjMat);

// 扩容后,指向新地址
t->adjMat = tempMat; // 指向新的邻接矩阵地址
t->adjMat = tempMat; // 指向新的邻接矩阵地址
t->capacity = t->size * 2;
t->size++;
}
Expand All @@ -113,7 +111,7 @@ void removeVertex(graphAdjMat *t, unsigned int index) {
for (int j = index; j < t->size - 1; j++) {
t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移
}
} else {
} else {
memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // 被删除行的下方所有行上移
for (int j = index; j < t->size; j++) {
t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移
Expand Down Expand Up @@ -156,12 +154,12 @@ void printGraph(graphAdjMat *t) {
/* 构造函数 */
graphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {
// 申请内存
graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存
newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存
graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存
newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存
unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存
newGraph->size = numberVertices; // 初始化顶点数量
newGraph->capacity = numberVertices * 2; // 初始化图容量
newGraph->size = numberVertices; // 初始化顶点数量
newGraph->capacity = numberVertices * 2; // 初始化图容量

// 配置二维数组
for (int i = 0; i < numberVertices * 2; i++) {
Expand Down
14 changes: 7 additions & 7 deletions codes/c/chapter_hashing/array_hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../utils/common.h"

/* 哈希表默认数组大小 */
# define HASH_MAP_DEFAULT_SIZE 100
#define HASH_MAP_DEFAULT_SIZE 100

/* 键值对 int->string */
struct pair {
Expand Down Expand Up @@ -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);
}
Expand All @@ -164,7 +164,7 @@ void print(ArrayHashMap *d) {
/* Driver Code */
int main() {
/* 初始化哈希表 */
ArrayHashMap * map = newArrayHashMap();
ArrayHashMap *map = newArrayHashMap();

/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
Expand All @@ -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);

/* 删除操作 */
Expand All @@ -196,20 +196,20 @@ 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]);
}
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]);
}
free(set.set);

return 0;
}
4 changes: 2 additions & 2 deletions codes/c/chapter_sorting/bubble_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
void bubbleSort(int nums[], int size) {
// 外循环:未排序区间为 [0, i]
for (int i = 0; i < size - 1; i++) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < size - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
Expand All @@ -26,7 +26,7 @@ void bubbleSortWithFlag(int nums[], int size) {
// 外循环:未排序区间为 [0, i]
for (int i = 0; i < size - 1; i++) {
bool flag = false;
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < size - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/selection_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void selectionSort(int nums[], int n) {
int k = i;
for (int j = i + 1; j < n; j++) {
if (nums[j] < nums[k])
k = j; // 记录最小元素的索引
k = j; // 记录最小元素的索引
}
// 将该最小元素与未排序区间的首个元素交换
int temp = nums[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int bubbleSort(vector<int> &nums) {
int count = 0; // 计数器
// 外循环:未排序区间为 [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_divide_and_conquer/hanota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -52,7 +52,7 @@ int main() {
cout << "C =";
printVector(C);

hanota(A, B, C);
solveHanota(A, B, C);

cout << "圆盘移动完成后:\n";
cout << "A =";
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_hashing/hash_map_chaining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HashMapChaining {
HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) {
buckets.resize(capacity);
}

/* 析构方法 */
~HashMapChaining() {
for (auto &bucket : buckets) {
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_hashing/simple_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_sorting/bubble_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
void bubbleSort(vector<int> &nums) {
// 外循环:未排序区间为 [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
Expand All @@ -26,7 +26,7 @@ void bubbleSortWithFlag(vector<int> &nums) {
// 外循环:未排序区间为 [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
bool flag = false; // 初始化标志位
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_backtracking/subset_sum_i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_backtracking;
namespace hello_algo.chapter_backtracking;

public class subset_sum_i {
/* 回溯算法:子集和 I */
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_backtracking/subset_sum_i_naive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_backtracking;
namespace hello_algo.chapter_backtracking;

public class subset_sum_i_naive {
/* 回溯算法:子集和 I */
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_backtracking/subset_sum_ii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_backtracking;
namespace hello_algo.chapter_backtracking;

public class subset_sum_ii {
/* 回溯算法:子集和 II */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_dynamic_programming;
namespace hello_algo.chapter_dynamic_programming;

public class climbing_stairs_backtrack {
/* 回溯 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_dynamic_programming;
namespace hello_algo.chapter_dynamic_programming;

public class climbing_stairs_dfs {
/* 搜索 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_dynamic_programming;
namespace hello_algo.chapter_dynamic_programming;

public class climbing_stairs_dp {
/* 爬楼梯:动态规划 */
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_dynamic_programming/min_path_sum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_dynamic_programming;
namespace hello_algo.chapter_dynamic_programming;

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];
}
// 若行列索引越界,则返回 +∞ 代价
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_hashing/built_in_hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_hashing;
namespace hello_algo.chapter_hashing;

public class built_in_hash {
[Test]
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_hashing/simple_hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_hashing;
namespace hello_algo.chapter_hashing;

public class simple_hash {
/* 加法哈希 */
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_heap/top_k.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_heap;
namespace hello_algo.chapter_heap;

public class top_k {
/* 基于堆查找数组中最大的 k 个元素 */
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_sorting/bucket_sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void bucketSort(float[] nums) {
// 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);
}
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_sorting/heap_sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: hpstory ([email protected])
*/

namespace hello_algo.chapter_sorting;
namespace hello_algo.chapter_sorting;

public class heap_sort {
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/utils/TreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public TreeNode(int x) {

/* 将列表反序列化为二叉树:递归 */
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);
Expand Down
4 changes: 2 additions & 2 deletions codes/dart/chapter_divide_and_conquer/hanota.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -45,7 +45,7 @@ void main() {
print("B = $B");
print("C = $C");

hanota(A, B, C);
solveHanota(A, B, C);

print("圆盘移动完成后:");
print("A = $A");
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_array_and_linkedlist/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ func TestList(t *testing.T) {
/* 排序列表 */
sort.Ints(list) // 排序后,列表元素从小到大排列
fmt.Println("排序列表后 list =", list)
}
}
Loading

0 comments on commit dd72335

Please sign in to comment.