Format the C code in Clang-Format Style: Microsoft

This commit is contained in:
krahets 2023-04-17 21:13:15 +08:00
parent 1d6b7a5644
commit 9a98ff8a5e
46 changed files with 215 additions and 216 deletions

2
codes/c/.gitignore vendored
View File

@ -5,3 +5,5 @@
# Unignore all dirs
!*/
*.dSYM/
build/

View File

@ -67,7 +67,6 @@ int find(int* nums, int size, int target) {
return -1;
}
/* Driver Code */
int main() {
/* 初始化数组 */

View File

@ -48,7 +48,6 @@ int find(ListNode* head, int target) {
return -1;
}
/* Driver Code */
int main() {
/* 初始化链表 */

View File

@ -113,6 +113,7 @@ int *toArray(myList *list) {
return list->nums;
}
/* Driver Code */
int main() {
/* 初始化列表 */
myList *list = newMyList();
@ -157,4 +158,6 @@ int main() {
/* 释放分配内存 */
delMyList(list);
return 0;
}

View File

@ -1 +1 @@
add_executable(linear_search linear_search.c)
add_executable(binary_search binary_search.c)

View File

@ -37,8 +37,7 @@ int arrayTraversal(int *nums, int n) {
}
/* 平方阶 */
int quadratic(int n)
{
int quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) {
@ -85,7 +84,8 @@ int exponential(int n) {
/* 指数阶(递归实现) */
int expRecur(int n) {
if (n == 1) return 1;
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
@ -101,15 +101,16 @@ int logarithmic(float n) {
/* 对数阶(递归实现) */
int logRecur(float n) {
if (n <= 1) return 0;
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
/* 线性对数阶 */
int linearLogRecur(float n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
@ -118,7 +119,8 @@ int linearLogRecur(float n) {
/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
if (n == 0) return 1;
if (n == 0)
return 1;
int count = 0;
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
@ -172,5 +174,6 @@ int main(int argc, char *argv[]) {
nums = NULL;
}
getchar();
return 0;
}

View File

@ -29,7 +29,8 @@ int findOne(int *nums, int n) {
for (int i = 0; i < n; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1) return i;
if (nums[i] == 1)
return i;
}
return -1;
}
@ -51,5 +52,6 @@ int main(int argc, char *argv[]) {
nums = NULL;
}
}
return 0;
}

View File

@ -49,7 +49,7 @@ int parent(maxHeap *h, int i) {
}
/* 交换元素 */
int swap(maxHeap *h, int i, int j) {
void swap(maxHeap *h, int i, int j) {
int temp = h->data[i];
h->data[i] = h->data[j];
h->data[j] = temp;
@ -71,7 +71,7 @@ int peek(maxHeap *h) {
}
/* 元素入堆 */
int push(maxHeap *h, int val) {
void push(maxHeap *h, int val) {
// 默认情况下,不应该添加这么多节点
if (h->size == MAX_SIZE) {
printf("heap is full!");
@ -104,7 +104,6 @@ int pop(maxHeap *h) {
return val;
}
/* 从节点 i 开始,从顶至底堆化 */
void siftDown(maxHeap *h, int i) {
while (true) {
@ -145,6 +144,7 @@ void siftUp(maxHeap *h, int i) {
}
}
/* Driver Code */
int main() {
/* 初始化堆 */
// 初始化大顶堆

View File

@ -1,2 +1 @@
add_executable(binary_search binary_search.c)
add_executable(leetcode_two_sum leetcode_two_sum.c)

View File

@ -66,6 +66,7 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
return NULL;
}
/* Driver Code */
int main() {
// ======= Test Case =======
int nums[] = {2, 7, 11, 15};

View File

@ -7,8 +7,7 @@
#include "../include/include.h"
/* 冒泡排序 */
void bucketSort(double nums[], int size)
{
void bucketSort(double nums[], int size) {
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
int k = size / 2;
// 1. 将数组元素分配到各个桶中
@ -23,18 +22,13 @@ void bucketSort(double nums[], int size)
}
/* Driver Code */
int main()
{
int main() {
// 设输入数据为浮点数,范围为 [0, 1)
double nums[] = {0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37};
int size = sizeof(nums) / sizeof(double);
bucketSort(nums, size);
printf("桶排序完成后 nums = ");
printf("[");
for (int i = 0; i < size - 1; i++)
{
printf("%g, ", nums[i]);
}
printf("]");
printArray(nums, size);
}

View File

@ -9,12 +9,10 @@
/* 插入排序 */
void insertionSort(int nums[], int size) {
// 外循环base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < size; i++)
{
for (int i = 1; i < size; i++) {
int base = nums[i], j = i - 1;
// 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > base)
{
while (j >= 0 && nums[j] > base) {
// 1. 将 nums[j] 向右移动一位
nums[j + 1] = nums[j];
j--;
@ -29,8 +27,7 @@ int main() {
int nums[] = {4, 1, 3, 1, 5, 2};
insertionSort(nums, 6);
printf("插入排序完成后 nums = ");
for (int i = 0; i < 6; i++)
{
for (int i = 0; i < 6; i++) {
printf("%d ", nums[i]);
}
printf("\n");

View File

@ -39,7 +39,8 @@ void merge(int *nums, int left, int mid, int right) {
/* 归并排序 */
void mergeSort(int *nums, int left, int right) {
// 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归
if (left >= right)
return; // 当子数组长度为 1 时终止递归
// 划分阶段
int mid = (left + right) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组
@ -56,5 +57,6 @@ int main() {
mergeSort(nums, 0, size - 1);
printf("归并排序完成后 nums = ");
printArray(nums, size);
return 0;
}

View File

@ -93,7 +93,6 @@ void quickSortMedian(int nums[], int left, int right) {
quickSortMedian(nums, pivot + 1, right);
}
/* 快速排序类(尾递归优化) */
/* 尾递归优化-哨兵划分 */
int quickSortTailCallPartition(int nums[], int left, int right) {
@ -110,7 +109,6 @@ int quickSortTailCallPartition(int nums[], int left, int right) {
return i; // 返回基准数的索引
}
// 快速排序(尾递归优化)
void quickSortTailCall(int nums[], int left, int right) {
// 子数组长度为 1 时终止
@ -128,7 +126,6 @@ void quickSortTailCall(int nums[], int left, int right) {
}
}
/* Driver Code */
int main() {
/* 快速排序 */

View File

@ -59,6 +59,7 @@ void radixSort(int nums[], int size) {
countingSortDigit(nums, size, exp);
}
/* Driver Code */
int main() {
// 基数排序
int nums[] = {10546151, 35663510, 42865989, 34862445, 81883077,

View File

@ -122,7 +122,6 @@ void printArrayDeque(ArrayDeque *deque) {
printArray(arr, deque->queSize);
}
/* Driver Code */
int main() {
/* 初始化队列 */

View File

@ -85,7 +85,6 @@ void printArrayQueue(ArrayQueue *queue) {
printArray(arr, queue->queSize);
}
/* Driver Code */
int main() {
/* 初始化队列 */

View File

@ -211,4 +211,3 @@ int main() {
delLinkedListdeque(deque);
return 0;
}

View File

@ -125,4 +125,3 @@ int main() {
return 0;
}

View File

@ -108,7 +108,8 @@ void removeNode(binarySearchTree *bst, int num) {
// 循环查找,越过叶节点后跳出
while (cur != NULL) {
// 找到待删除节点,跳出循环
if (cur->val == num) break;
if (cur->val == num)
break;
pre = cur;
if (cur->val < num) {
// 待删除节点在 root 的右子树中
@ -159,13 +160,11 @@ int main() {
TreeNode *node = search(bst, 7);
printf("查找到的节点对象的节点值 = %d\n", node->val);
/* 插入节点 */
insert(bst, 16);
printf("插入节点 16 后,二叉树为\n");
printTree(getRoot(bst));
/* 删除节点 */
removeNode(bst, 1);
printf("删除节点 1 后,二叉树为\n");

View File

@ -45,7 +45,6 @@ int *levelOrder(TreeNode *root, int *size) {
return arr;
}
/* Driver Code */
int main() {
/* 初始化二叉树 */

View File

@ -11,7 +11,8 @@ int *arr;
/* 前序遍历 */
void preOrder(TreeNode *root, int *size) {
if (root == NULL) return;
if (root == NULL)
return;
// 访问优先级:根节点 -> 左子树 -> 右子树
arr[(*size)++] = root->val;
preOrder(root->left, size);
@ -20,7 +21,8 @@ void preOrder(TreeNode *root, int *size) {
/* 中序遍历 */
void inOrder(TreeNode *root, int *size) {
if (root == NULL) return;
if (root == NULL)
return;
// 访问优先级:左子树 -> 根节点 -> 右子树
inOrder(root->left, size);
arr[(*size)++] = root->val;
@ -29,14 +31,14 @@ void inOrder(TreeNode *root, int *size) {
/* 后序遍历 */
void postOrder(TreeNode *root, int *size) {
if (root == NULL) return;
if (root == NULL)
return;
// 访问优先级:左子树 -> 右子树 -> 根节点
postOrder(root->left, size);
postOrder(root->right, size);
arr[(*size)++] = root->val;
}
/* Driver Code */
int main() {
/* 初始化二叉树 */

View File

@ -7,16 +7,16 @@
#ifndef C_INCLUDE_H
#define C_INCLUDE_H
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <assert.h>
#include "list_node.h"
#include "tree_node.h"
#include "print_util.h"
#include "tree_node.h"
// hash table lib
#include "uthash.h"

View File

@ -8,8 +8,8 @@
#define PRINT_UTIL_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include "list_node.h"
#include "tree_node.h"
@ -18,7 +18,6 @@
extern "C" {
#endif
/**
* @brief Print an Array
*
@ -146,7 +145,6 @@ static void printHeap(int arr[], int size) {
printTree(root);
}
#ifdef __cplusplus
}
#endif

View File

@ -4,7 +4,6 @@
* Author: Reanon (793584285@qq.com)
*/
#ifndef TREE_NODE_H
#define TREE_NODE_H
@ -24,7 +23,6 @@ struct TreeNode {
typedef struct TreeNode TreeNode;
TreeNode *newTreeNode(int val) {
TreeNode *node;
@ -83,7 +81,6 @@ TreeNode *arrToTree(const int *arr, size_t size) {
return root;
}
/**
* @brief Generate a binary tree with an array
*

View File

@ -53,6 +53,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
}
}
/* Driver Code */
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;

View File

@ -27,6 +27,7 @@ static void preOrder(TreeNode *root) {
path.pop_back();
}
/* Driver Code */
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;

View File

@ -21,6 +21,7 @@ static void preOrder(TreeNode *root) {
preOrder(root->right);
}
/* Driver Code */
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;

View File

@ -26,6 +26,7 @@ static void preOrder(TreeNode *root) {
path.pop_back();
}
/* Driver Code */
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;

View File

@ -19,6 +19,7 @@ void testPop(priority_queue<int> &heap) {
printHeap(heap);
}
/* Driver Code */
int main() {
/* 初始化堆 */
// 初始化小顶堆

View File

@ -34,6 +34,7 @@ vector<int> twoSumHashTable(vector<int> &nums, int target) {
return {};
}
/* Driver Code */
int main() {
// ======= Test Case =======
vector<int> nums = {2, 7, 11, 15};

View File

@ -4,6 +4,7 @@
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
/* Driver Code */
int main() {
/* 初始化列表 */
List<int> list = [1,3,2,5,4];

View File

@ -121,6 +121,7 @@ int factorialRecur(int n) {
return count;
}
/* Driver Code */
int main() {
// n
int n = 8;

View File

@ -27,6 +27,7 @@ int findOne(List<int> nums) {
return -1;
}
/* Driver Code */
int main() {
for (var i = 0; i < 10; i++) {
int n = 100;

View File

@ -1,4 +1,4 @@
/*
/**
* File: worst_best_time_complexity.js
* Created Time: 2023-01-05
* Author: RiverTwilight (contact@rene.wang)

View File

@ -1,4 +1,4 @@
/*
/**
* File: binary_search.ts
* Created Time: 2022-12-27
* Author: Daniel (better.sunjian@gmail.com)

View File

@ -1,4 +1,4 @@
/*
/**
* File: worst_best_time_complexity.ts
* Created Time: 2023-01-05
* Author: RiverTwilight (contact@rene.wang)