Several bug fixes.

This commit is contained in:
krahets 2023-10-29 00:09:54 +08:00
parent c37f0981f0
commit db5d1d21f3
9 changed files with 14 additions and 17 deletions

View File

@ -13,7 +13,7 @@ TreeNode *res[MAX_SIZE];
int resSize = 0; int resSize = 0;
/* 前序遍历:例题一 */ /* 前序遍历:例题一 */
static void preOrder(TreeNode *root) { void preOrder(TreeNode *root) {
if (root == NULL) { if (root == NULL) {
return; return;
} }

View File

@ -15,7 +15,7 @@ TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
int pathSize = 0, resSize = 0; int pathSize = 0, resSize = 0;
/* 前序遍历:例题二 */ /* 前序遍历:例题二 */
static void preOrder(TreeNode *root) { void preOrder(TreeNode *root) {
if (root == NULL) { if (root == NULL) {
return; return;
} }

View File

@ -32,7 +32,7 @@ typedef struct {
Node **buckets; // 桶数组 Node **buckets; // 桶数组
} HashMapChaining; } HashMapChaining;
/* 构造方法 */ /* 构造函数 */
HashMapChaining *initHashMapChaining() { HashMapChaining *initHashMapChaining() {
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining)); HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
hashMap->size = 0; hashMap->size = 0;
@ -46,7 +46,7 @@ HashMapChaining *initHashMapChaining() {
return hashMap; return hashMap;
} }
/* 析构方法 */ /* 析构函数 */
void freeHashMapChaining(HashMapChaining *hashMap) { void freeHashMapChaining(HashMapChaining *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
Node *cur = hashMap->buckets[i]; Node *cur = hashMap->buckets[i];

View File

@ -25,7 +25,7 @@ typedef struct {
// 函数声明 // 函数声明
void extend(HashMapOpenAddressing *hashMap); void extend(HashMapOpenAddressing *hashMap);
/* 构造方法 */ /* 构造函数 */
HashMapOpenAddressing *newHashMapOpenAddressing() { HashMapOpenAddressing *newHashMapOpenAddressing() {
HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing)); HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));
hashMap->size = 0; hashMap->size = 0;
@ -40,7 +40,7 @@ HashMapOpenAddressing *newHashMapOpenAddressing() {
return hashMap; return hashMap;
} }
/* 析构方法 */ /* 析构函数 */
void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) { void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
Pair *pair = hashMap->buckets[i]; Pair *pair = hashMap->buckets[i];

View File

@ -6,13 +6,13 @@
#include "../utils/common.h" #include "../utils/common.h"
/* 数组表示下的二叉树结构 */ /* 数组表示下的二叉树结构 */
typedef struct { typedef struct {
int *tree; int *tree;
int size; int size;
} ArrayBinaryTree; } ArrayBinaryTree;
/* 构造方法 */ /* 构造函数 */
ArrayBinaryTree *createArrayBinaryTree(int *arr, int arrSize) { ArrayBinaryTree *createArrayBinaryTree(int *arr, int arrSize) {
ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree)); ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
abt->tree = malloc(sizeof(int) * arrSize); abt->tree = malloc(sizeof(int) * arrSize);
@ -155,7 +155,7 @@ int main() {
free(res); free(res);
// 释放内存 // 释放内存
free(root); freeMemoryTree(root);
free(abt); free(abt);
return 0; return 0;

View File

@ -19,7 +19,7 @@ typedef struct ListNode {
/* 构造函数,初始化一个新节点 */ /* 构造函数,初始化一个新节点 */
ListNode *newListNode(int val) { ListNode *newListNode(int val) {
ListNode *node, *next; ListNode *node;
node = (ListNode *)malloc(sizeof(ListNode)); node = (ListNode *)malloc(sizeof(ListNode));
node->val = val; node->val = val;
node->next = NULL; node->next = NULL;

View File

@ -12,7 +12,7 @@ class ListNode {
ListNode(this.val, [this.next]); ListNode(this.val, [this.next]);
} }
/* Generate a linked list with a vector */ /* Generate a linked list with a list */
ListNode? listToLinkedList(List<int> list) { ListNode? listToLinkedList(List<int> list) {
ListNode dum = ListNode(0); ListNode dum = ListNode(0);
ListNode? head = dum; ListNode? head = dum;

View File

@ -14,14 +14,11 @@ from modules import Vertex, print_matrix
class GraphAdjMat: class GraphAdjMat:
"""基于邻接矩阵实现的无向图类""" """基于邻接矩阵实现的无向图类"""
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
vertices: list[int] = []
# 邻接矩阵,行列索引对应“顶点索引”
adj_mat: list[list[int]] = []
def __init__(self, vertices: list[int], edges: list[list[int]]): def __init__(self, vertices: list[int], edges: list[list[int]]):
"""构造方法""" """构造方法"""
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
self.vertices: list[int] = [] self.vertices: list[int] = []
# 邻接矩阵,行列索引对应“顶点索引”
self.adj_mat: list[list[int]] = [] self.adj_mat: list[list[int]] = []
# 添加顶点 # 添加顶点
for val in vertices: for val in vertices:

View File

@ -9,7 +9,7 @@ import utils
/* k */ /* k */
func topKHeap(nums: [Int], k: Int) -> [Int] { func topKHeap(nums: [Int], k: Int) -> [Int] {
// k // k
var heap = Heap(nums.prefix(k)) var heap = Heap(nums.prefix(k))
// k+1 k // k+1 k
for i in stride(from: k, to: nums.count, by: 1) { for i in stride(from: k, to: nums.count, by: 1) {