diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo index 6177be5..acef311 100644 Binary files a/Alogrithm/.vs/Alogrithm/v16/.suo and b/Alogrithm/.vs/Alogrithm/v16/.suo differ diff --git a/Alogrithm/Alogrithm/config/15_WordPattern.ini b/Alogrithm/Alogrithm/config/15_WordPattern.ini new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/config/4_IsPalindrome.ini b/Alogrithm/Alogrithm/config/4_IsPalindrome.ini index 1730509..e1b8877 100644 --- a/Alogrithm/Alogrithm/config/4_IsPalindrome.ini +++ b/Alogrithm/Alogrithm/config/4_IsPalindrome.ini @@ -15,4 +15,7 @@ Na=20 nExpect=0 [Test6] Na=-214748368 +nExpect=0 +[Test7] +Na=-123321 nExpect=0 \ No newline at end of file diff --git a/Alogrithm/Alogrithm/include/15_WordPattern.h b/Alogrithm/Alogrithm/include/15_WordPattern.h new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/include/3_bool IsUgly.h b/Alogrithm/Alogrithm/include/3_bool IsUgly.h index e051f80..b0585bf 100644 --- a/Alogrithm/Alogrithm/include/3_bool IsUgly.h +++ b/Alogrithm/Alogrithm/include/3_bool IsUgly.h @@ -1,2 +1,3 @@ #pragma once +#include bool IsUgly(int num); \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/10_BinaryTreePaths.cpp b/Alogrithm/Alogrithm/src/10_BinaryTreePaths.cpp index b6a5f01..2074469 100644 --- a/Alogrithm/Alogrithm/src/10_BinaryTreePaths.cpp +++ b/Alogrithm/Alogrithm/src/10_BinaryTreePaths.cpp @@ -1,6 +1,5 @@ #include "../include/10_BinaryTreePaths.h" - void CreatBitTreeNode3(char str[][50], int return_count, TreeNode3* cur, int curIndex) { if (str == NULL || return_count <= 0 || cur == NULL || curIndex >= return_count || curIndex < 0) { @@ -43,34 +42,40 @@ TreeNode3* CreatBitTree3(char str[][50], int return_count) CreatBitTreeNode3(str, return_count, head, 0); return head; } + void get_path(char** array, struct TreeNode3* root, int* returnSize, int* buf, int local) { - if (NULL == root) { + if (NULL == root) {//如果根节点为NULL,直接返回 return; } - if (!root->left && !root->right) { + if (NULL == root->left && NULL == root->right) {//如果左右子树都为空 + //说明找到路了,把缓冲区的打印出来即可 char* str = (char*)malloc(1024); - int len = 0; - for (int i = 0; i < local; i++) - { - len += sprintf(str + len, "%d->", buf[i]); - } - sprintf(str + len, "%d", root->val); - array[(*returnSize)++] = str; + int len = 0; //这里len作为偏移量 + for (int i = 0; i < local; i++) //把找到的一条路径拼接起来, + { + len += sprintf(str + len, "%d->", buf[i]);//sprintf返回成功写入字符个数 + } + sprintf(str + len, "%d", root->val);//拼接叶节点的最后一个值 + array[(*returnSize)++] = str; //把拼接好的字符串地址放到要返回的array中 } - else { + else {//如果左右子树不为空 + // 把当前的值写进buf,层级+1,继续递归找路 buf[local++] = root->val; get_path(array, root->left, returnSize, buf, local); get_path(array, root->right, returnSize, buf, local); } } +//题目:给定一个二叉树,返回所有从根节点到叶子节点的路径。 +//思路:递归遍历二叉树,利用sprintf函数对路径进行拼接,拼接的路径作为字符串存放在二维字符串数组中 char** binaryTreePaths(struct TreeNode3* root, int* returnSize) { char** ret = (char**)malloc(sizeof(char*) * 1024);//定义一个二级指针,作为返回值返回二维字符串数组 *returnSize = 0;//用来保存二维字符串数组的元素个数 - int buf[1024] = { 0 };//用来接收缓冲 - get_path(ret, root, returnSize, buf, 0); + int buf[1024] = { 0 };//用来接收缓冲,树中的val + get_path(ret, root, returnSize, buf, 0);//调用函数,对二叉树进行递归遍历 return ret; } +//释放二叉树 void free_tree3(TreeNode3* T)//后序释放 { if (!T) { diff --git a/Alogrithm/Alogrithm/src/11_CanWinNim.cpp b/Alogrithm/Alogrithm/src/11_CanWinNim.cpp index bf39f63..7ab9fe4 100644 --- a/Alogrithm/Alogrithm/src/11_CanWinNim.cpp +++ b/Alogrithm/Alogrithm/src/11_CanWinNim.cpp @@ -1,5 +1,6 @@ #include "../include/11_CanWinNim.h" - +//题目: Nim 游戏 +//思路:只要不是4的倍数,都可以赢得游戏,换句话说就是对手拿完石子以后,还剩四颗石子,则必输 bool CanWinNim(int n) { if (n % 4 != 0) { return true; diff --git a/Alogrithm/Alogrithm/src/12_IsValid.cpp b/Alogrithm/Alogrithm/src/12_IsValid.cpp index 8782f62..8ef6947 100644 --- a/Alogrithm/Alogrithm/src/12_IsValid.cpp +++ b/Alogrithm/Alogrithm/src/12_IsValid.cpp @@ -1,7 +1,9 @@ #include "../include/12_IsValid.h" #pragma warning(disable:6385) #pragma warning(disable:6386) -//思路:让左括号进栈,右括号和左括号依次进行匹配。 +//题目:括号匹配问题 +//思路:定义一个标志位,让左括号进栈,标志位移动,之后右括号和左括号依次进行匹配, +// 匹配上了标志位移动,最后判断标志位是否回到原位。 bool IsValid(char* s) { int s_len = strlen(s);//定义一个int变量,存放字符串长度 char* stack = (char*)malloc(sizeof(char) * (s_len + 2));//定义一个char*指针用来模拟一个栈 diff --git a/Alogrithm/Alogrithm/src/13_MyAtoi.cpp b/Alogrithm/Alogrithm/src/13_MyAtoi.cpp index f80f3b7..7481dc2 100644 --- a/Alogrithm/Alogrithm/src/13_MyAtoi.cpp +++ b/Alogrithm/Alogrithm/src/13_MyAtoi.cpp @@ -1,5 +1,7 @@ #include "../include/13_MyAtoi.h" - +//题目:字符串转整数 +//思路:先对无用的开头空格字符、正负号、非空格和非数字开头进行判断,是则返回0 +// 再对字符串遍历,相加得到整数。 int MyAtoi(char* s) { int Index = 0;//用来遍历字符串s,作为下标使用 int flag = 1;//符号标志位,>0则读到正号,<0则读到负号 diff --git a/Alogrithm/Alogrithm/src/14_SingleNumber.cpp b/Alogrithm/Alogrithm/src/14_SingleNumber.cpp index 4f58fbb..e668e9e 100644 --- a/Alogrithm/Alogrithm/src/14_SingleNumber.cpp +++ b/Alogrithm/Alogrithm/src/14_SingleNumber.cpp @@ -1,6 +1,6 @@ #include "../include/14_SingleNumber.h" - +//题目:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 //思路:使用异或,异或的性质: // 1.0异或任何数=任何数 // 2.任何数异或自己=把自己置0 diff --git a/Alogrithm/Alogrithm/src/15_WordPattern.cpp b/Alogrithm/Alogrithm/src/15_WordPattern.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp b/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp index d3c605b..18cbaea 100644 --- a/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp +++ b/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp @@ -30,13 +30,12 @@ bool ContainsNearbyDuplicate(int* nums, int numsSize, int k) node* arr = NULL; //定义一个node 指针 int i = 0; int j = 0; - arr = (node*)malloc(sizeof(node) * 100000);//为arr指针分配足够空间,用来保存数组 + arr = (node*)malloc(sizeof(node) * numsSize);//为arr指针分配足够空间,用来保存数组 if (NULL == arr) { printf("内存分配失败\n"); } - else - { + else{ for (i = 0; i < numsSize; i++)//遍历数组nums,把nums里的值和对应的下标保存在arr中 { arr[i].value = nums[i]; diff --git a/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp b/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp index e216b2e..11707e9 100644 --- a/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp +++ b/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp @@ -2,16 +2,16 @@ //给定一个整数,返回它在 Excel 表中相对应的列名称,如果在Excel表中找不到该列,则返回""。 char* ExcelSheetColumnTitle(int n) -{ - char* ret = (char*)malloc(sizeof(char) * 10);//开辟一片空间,存需要返回的字符串 - int i = 9;// - while (n) +{ //int类型大于0的数为0~2147483647,可以用7个A~Z的字母表示,int不会超过26的7次方 + char* ret = (char*)malloc(sizeof(char) * 8);//所以这里申请8字节的空间,一个字节存放\0标志 + int i = 7;//用来访问i字符串中的每个字符 + ret[7] = '\0';//给结尾赋上\0标志位 + while (n)//当n等于0时结束 { - n -= 1;//给n减一,因为n后面是从'A'开始的 - ret[i - 1] = n % 26 + 'A';//从后往前赋值,从最后一个赋值到第一个 + n -= 1;//给n减一,因为n后面是从字符'A'开始的相加的 + ret[i-1] = n % 26 + 'A';//从后往前赋值,从最后一个赋值到第一个 n /= 26; - i--;//给下一个赋值 + i--;//给下一个字符赋值 } - ret[9] = '\0'; return ret + i;//去掉前面的前导字符,如果前面没有字符则指针后移 } \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/3_bool IsUgly.cpp b/Alogrithm/Alogrithm/src/3_bool IsUgly.cpp index b18b055..4c8bd01 100644 --- a/Alogrithm/Alogrithm/src/3_bool IsUgly.cpp +++ b/Alogrithm/Alogrithm/src/3_bool IsUgly.cpp @@ -1,6 +1,8 @@ -#include + #include "../include/3_bool IsUgly.h " //判断一个数是否为丑数(能被2 3 5整除的数),能返回true,不能返回false +//思路:对入参循环判断%2、%3、%5是否为0,是则除相应的数,否则跳出循环,对num判断, +//为1,则说明能被2、3、5除尽,是丑数,否则不是。 bool IsUgly(int num) { //入参判断,如果小于等于0直接返回false if (num <= 0) { diff --git a/Alogrithm/Alogrithm/src/4_IsPalindrome.cpp b/Alogrithm/Alogrithm/src/4_IsPalindrome.cpp index bd06958..4581747 100644 --- a/Alogrithm/Alogrithm/src/4_IsPalindrome.cpp +++ b/Alogrithm/Alogrithm/src/4_IsPalindrome.cpp @@ -1,12 +1,14 @@ #include "../include/4_IsPalindrome.h" - +//题目:判断一个数是否为回文数 //思路:对传进来的数先知道是几位数,再循环%10,再/10,取到每位上的数字,再逆序*10相加,得到翻转后的数 //最后再和原数作对比。 bool IsPalindrome(int x) { + if (x < 0)//如果入参为负数,则不是回文数,返回false + return false; int x_Count = 1;//定义一个count用来统计传进来的数是几位数 - long long x_Coln1 = x;//把传进来的数拷贝一份,后面会用到 + int x_Coln1 = x;//把传进来的数拷贝一份,因为后面对要对x做除法运算 long long x_Turn = 0;//用来保存翻转后的值,考虑int翻转后溢出问题 while ((x / 10) != 0) {//通过循环来获得入参是几位数 x_Count++; @@ -14,15 +16,11 @@ bool IsPalindrome(int x) } x = x_Coln1;//保存原数,后面还要用到 for (int i = 0; i < x_Count; i++) {//把逆置后的数存放到x_Turn中(保存的是正数) - int temp = x % 10; - x_Turn = x_Turn * 10 + temp; - x = x / 10; + int temp = x % 10;//取整数的个位数字 + x_Turn = x_Turn * 10 + temp;//取到的数字逆序相加 + x = x / 10;//除10,继续取个位数字 } - if (x_Coln1 < 0) {//判断入参是否为负数,是的话加上负号,不是则不用变 - x_Turn = 0 - x_Turn; - } - if (x_Turn == x_Coln1) {//判断翻转后的数和原数是否相等 + if (x_Turn == x_Coln1) //判断翻转后的数和原数是否相等 return true; - } return false; } \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/5_MinDepth.cpp b/Alogrithm/Alogrithm/src/5_MinDepth.cpp index 704e717..358aed8 100644 --- a/Alogrithm/Alogrithm/src/5_MinDepth.cpp +++ b/Alogrithm/Alogrithm/src/5_MinDepth.cpp @@ -1,23 +1,27 @@ #include "../include/5_MinDepth.h" +//题目:求一个二叉树的最小深度 +//思路:递归遍历这棵树 +//创建一棵二叉树,入参为字符串数组。 TreeNode* CreatBitTree(char str[][50], int return_count) -{ +{ //如果传进来的字符串数组为空,则返回空 if (str == NULL || return_count <= 0) { return NULL; - } + }//给给根节点开辟一片空间 TreeNode* head = (TreeNode*)malloc(sizeof(TreeNode)); - if (NULL == head) { + if (NULL == head) {//如果内存申请失败,返回NULL return NULL; - } + }//给根节点的数据域赋值, head->val = _ttoi(str[0]); - CreatBitTreeNode1(str, return_count, head, 0); - return head; + CreatBitTreeNode1(str, return_count, head, 0);//通过调用函数,给根节点以外的子树赋值 + return head;//返回根节点 } +//函数功能是给根节点以外的子树赋值,入参为字符串数组、根节点、标志位 void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* cur, int curIndex) -{ +{ //赋值前判空 if (str == NULL || return_count <= 0 || cur == NULL || curIndex >= return_count || curIndex < 0) { return; - } + }//递归赋值 int last = return_count - 1; if ((2 * curIndex + 1 <= last) && strcmp(str[2 * curIndex + 1], "NULL")) { cur->left = (TreeNode*)malloc(sizeof(TreeNode)); @@ -42,29 +46,32 @@ void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* cur, int curI CreatBitTreeNode1(str, return_count, cur->left, 2 * curIndex + 1); CreatBitTreeNode1(str, return_count, cur->right, 2 * curIndex + 2); } +//找二叉树的最小深度 int MinDepth(struct TreeNode* root) { int Depth = 0; int L1, L2; - if (NULL == root) { + if (NULL == root) {//如果根节点为空,返回0 return 0; - } + }//如果左右子树为空,返回1 else if ((NULL == root->left) && (NULL == root->right)) { return 1; - } + }//如果左子树不为空,右子树为空,继续搜索左子树 else if ((NULL != root->left) && (NULL == root->right)) { Depth = MinDepth(root->left); - } + }//如果左子树为空,右子树不为空,继续搜索右子树 else if ((NULL == root->left) && (NULL != root->right)) { Depth = MinDepth(root->right); - } + }//如果都不为空,则两边搜索 else { L1 = MinDepth(root->left); L2 = MinDepth(root->right); - Depth = fmin(L1, L2); + Depth = fmin(L1, L2);//把左右子树中,深度较小的一个给Depth } - return Depth + 1; + return Depth + 1;//每次搜索完,把搜索到的结果进行累加 } + +//释放二叉树 void free_tree(TreeNode* T)//后序释放 { if (!T) { diff --git a/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp b/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp index e3cf1be..5bb99f5 100644 --- a/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp +++ b/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp @@ -1,16 +1,19 @@ #include"../include/6_ContainsDuplicate.h" - +//题目:给定一个整数数组,判断是否存在重复元素。如果任意一值在数组中出现至少两次,函数返回true 。 +// 如果数组中每个元素都不相同,则返回false 。 +//思路:先排序,然后比较相邻元素是否相等,若相等则返回true,否则返回false。 +//qsort比较函数,返回值小于0,升序;返回值大于0,降序 int cmp_6(const void* _a, const void* _b) { int a = *(int*)_a, b = *(int*)_b; return a - b; } -//6.排序,比较相邻元素是否相等,若相等则返回true,否则返回false。 + bool containsDuplicate(int* nums, int numsSize) { - qsort(nums, numsSize, sizeof(int), cmp_6); - for (int i = 0; i < numsSize - 1; i++) { - if (nums[i] == nums[i + 1]) { + qsort(nums, numsSize, sizeof(int), cmp_6);//对传进来的数组排序 + for (int i = 0; i < numsSize - 1; i++) {//遍历数组, + if (nums[i] == nums[i + 1]) {//对相邻的两个元素作比较,如果相等返回true; return true; } } - return false; + return false;//不相等,返回false } \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/7_MaxDepth.cpp b/Alogrithm/Alogrithm/src/7_MaxDepth.cpp index 52af908..3b1b9b1 100644 --- a/Alogrithm/Alogrithm/src/7_MaxDepth.cpp +++ b/Alogrithm/Alogrithm/src/7_MaxDepth.cpp @@ -42,28 +42,29 @@ void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* cur, int cur CreatBitTreeNode2(str, return_count, cur->left, 2 * curIndex + 1); CreatBitTreeNode2(str, return_count, cur->right, 2 * curIndex + 2); } +//返回二叉树中,最大深度 int MaxDepth(struct TreeNode2* root) { int Depth = 0; int L1, L2; - if (NULL == root) { + if (NULL == root) {//根节点为NULL,返回0 return 0; - } + }//左右子树都为NULL,返回1 else if ((NULL == root->left) && (NULL == root->right)) { return 1; - } + }//左子树不为NULL,右子树为NULL,继续搜索左子树 else if ((NULL != root->left) && (NULL == root->right)) { Depth = MaxDepth(root->left); - } + }//右子树不为NUULL,左子树为NULL,继续搜索右子树 else if ((NULL == root->left) && (NULL != root->right)) { Depth = MaxDepth(root->right); - } + }//如果左右子树都不为NULL,左右子树继续搜索 else { L1 = MaxDepth(root->left); L2 = MaxDepth(root->right); - Depth = fmax(L1, L2); + Depth = fmax(L1, L2);//返回左右子树中,较大的一个 } - return Depth + 1; + return Depth + 1;//每次搜索完,对Depth进行累加,最后返回。 } void free_tree2(TreeNode2* T)//后序释放 { diff --git a/Alogrithm/Alogrithm/src/8_HammingWeight.cpp b/Alogrithm/Alogrithm/src/8_HammingWeight.cpp index 23febbb..38d10f2 100644 --- a/Alogrithm/Alogrithm/src/8_HammingWeight.cpp +++ b/Alogrithm/Alogrithm/src/8_HammingWeight.cpp @@ -1,12 +1,13 @@ #include"../include/8_HammingWeight.h" -//8.定义一个count,记录1的个数,把入参和1与操作,如果为1,count加1;如果为0,count加0 -//把入参右移一位,循环上面的操作,直到n=0时结束。 +//题目:编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为‘1’的个数(也被称为汉明重量)。 +//思路:定义一个count,记录1的个数,把入参和1与操作,如果为1,count加1;如果为0,count加0 + int HammingWeight(uint32_t n) { int n_Count = 0; while (n) { - n_Count += n & 1; - n >>= 1; + n_Count += n & 1;//n & 1的结果为1或者0,1说明最后一位为1,0说明最后一位为0 + n >>= 1;//把入参右移一位,循环上面的操作,直到n=0时结束。 } return n_Count; } \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/9_AddBinary.cpp b/Alogrithm/Alogrithm/src/9_AddBinary.cpp index fedc668..82411c9 100644 --- a/Alogrithm/Alogrithm/src/9_AddBinary.cpp +++ b/Alogrithm/Alogrithm/src/9_AddBinary.cpp @@ -1,12 +1,12 @@ #include"../include/9_AddBinary.h" - +//题目:给你两个二进制字符串,返回它们的和(用二进制表示)。 +//思路:获得两个字符串长度,定义一个进位标志位,从字符串的后面往前加 char* AddBinary(char* a, char* b) { int len_a = strlen(a) - 1;//存放字符串a的长度 int len_b = strlen(b) - 1;//存放字符串b的长度 int carry = 0;//进位标志 int len = len_a > len_b ? len_a : len_b;//获取两个字符串中长度较长的哪一个 - char* pstr = (char*)malloc(sizeof(char) * (len + 3));//定义一个指针作为返回值,多分配两个空间是为了防止有进位和字符结尾标志\0 if (NULL == pstr) return NULL; @@ -22,7 +22,7 @@ char* AddBinary(char* a, char* b) } //判断要不要加前导0 if (carry == 0) { - return (pstr + 1);//指针加一,表示指针向后移动,在这里如果不进位的话指针就指向非0的下一位。 + return (pstr + 1);//指针加一,表示指针向后移动, } else { pstr[0] = '1'; diff --git a/Alogrithm/Alogrithm/src/main.cpp b/Alogrithm/Alogrithm/src/main.cpp index 6c0e666..b1b8c8a 100644 --- a/Alogrithm/Alogrithm/src/main.cpp +++ b/Alogrithm/Alogrithm/src/main.cpp @@ -1,9 +1,7 @@ #include -#include "../include/13_MyAtoi.h" -#include "../include/14_SingleNumber.h" +#include "../include/4_IsPalindrome.h" int main() { - int arr[] = { -1,-2,-3,-4,-4,-3,-2,-1,0 }; - printf("%d\n", SingleNumber(arr, sizeof(arr)/sizeof(int))); + printf("%d\n",IsPalindrome(-123321)); return 0; }