From 4e745cd1f1a5bba495f0b20dc7664f8a09516c17 Mon Sep 17 00:00:00 2001 From: linyongji Date: Sat, 16 Jan 2021 16:49:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=94=AF5=E5=AE=8C=E6=88=90=E4=BA=86?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=EF=BC=8C=E5=B9=B6=E5=81=9A=E4=BA=86=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Alogrithm/.vs/Alogrithm/v16/.suo | Bin 62464 -> 71168 bytes Alogrithm/Alogrithm/Alogrithm.vcxproj | 5 +- Alogrithm/Alogrithm/Alogrithm.vcxproj.filters | 9 ++ Alogrithm/Alogrithm/config/5_MinDepth.ini | 18 ++++ .../Alogrithm/config/6_ContainsDuplicate.ini | 0 Alogrithm/Alogrithm/include/5_MinDepth.h | 18 ++++ .../Alogrithm/include/6_ContainsDuplicate.h | 0 Alogrithm/Alogrithm/src/5_MinDepth.cpp | 83 ++++++++++++++++++ .../Alogrithm/src/6_ContainsDuplicate.cpp | 0 Alogrithm/UnitTest/UnitTest.cpp | 20 +++++ Alogrithm/UnitTest/UnitTest.vcxproj | 2 +- Alogrithm/UnitTest/pch.cpp | 22 ++++- Alogrithm/UnitTest/pch.h | 3 + 13 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 Alogrithm/Alogrithm/config/5_MinDepth.ini create mode 100644 Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini create mode 100644 Alogrithm/Alogrithm/include/5_MinDepth.h create mode 100644 Alogrithm/Alogrithm/include/6_ContainsDuplicate.h create mode 100644 Alogrithm/Alogrithm/src/5_MinDepth.cpp create mode 100644 Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo index 429e3885a9fa73c3178eff991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151,12 +152,14 @@ + + diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters index 1b932e1..3d3b3bc 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters @@ -39,6 +39,9 @@ 源文件\src + + 源文件\src + @@ -53,6 +56,9 @@ 头文件\include + + 头文件\include + @@ -67,5 +73,8 @@ 资源文件\config + + 资源文件\config + \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/5_MinDepth.ini b/Alogrithm/Alogrithm/config/5_MinDepth.ini new file mode 100644 index 0000000..5fa6d8b --- /dev/null +++ b/Alogrithm/Alogrithm/config/5_MinDepth.ini @@ -0,0 +1,18 @@ +[Test1] +input=3,9,20,NULL,NULL,15,7 +output=2 +[Test2] +input=4,3,1,2,NULL,6,8 +output=3 +[Test3] +input=4,3,1,NULL,2,6,8 +output=3 +[Test5] +input=NULL +output=1 +[Test6] +input= +output=0 +[Test7] +input=1,0,0,2,NULL,NULL,0 +output=3 \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini b/Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/include/5_MinDepth.h b/Alogrithm/Alogrithm/include/5_MinDepth.h new file mode 100644 index 0000000..8ad75fd --- /dev/null +++ b/Alogrithm/Alogrithm/include/5_MinDepth.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include + + +struct TreeNode +{ + int val; + struct TreeNode* left; + struct TreeNode* right; +}; + + +int MinDepth(struct TreeNode* root); +TreeNode* CreatBitTree(char str[][50], int return_count); +void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* cur, int curIndex); +void free_tree(TreeNode* T); diff --git a/Alogrithm/Alogrithm/include/6_ContainsDuplicate.h b/Alogrithm/Alogrithm/include/6_ContainsDuplicate.h new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/src/5_MinDepth.cpp b/Alogrithm/Alogrithm/src/5_MinDepth.cpp new file mode 100644 index 0000000..704e717 --- /dev/null +++ b/Alogrithm/Alogrithm/src/5_MinDepth.cpp @@ -0,0 +1,83 @@ +#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) { + return NULL; + } + head->val = _ttoi(str[0]); + 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)); + if (NULL == cur->left) { + return; + } + cur->left->val = _ttoi(str[2 * curIndex + 1]); + } + else { + cur->left = NULL; + } + if ((2 * curIndex + 2 <= last) && strcmp(str[2 * curIndex + 2], "NULL")) { + cur->right = (TreeNode*)malloc(sizeof(TreeNode)); + if (NULL == cur->right) { + return; + } + cur->right->val = _ttoi(str[2 * curIndex + 2]); + } + else { + cur->right = NULL; + } + 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) { + return 0; + } + 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); + } + return Depth + 1; +} +void free_tree(TreeNode* T)//ͷ +{ + if (!T) { + return; + } + if (T->left) { + free_tree(T->left); + } + if (T->right) { + free_tree(T->right); + } + if (T) { + free(T); + T = NULL; + } +} \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp b/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp index 08d0654..7fc0a00 100644 --- a/Alogrithm/UnitTest/UnitTest.cpp +++ b/Alogrithm/UnitTest/UnitTest.cpp @@ -6,6 +6,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #define FileName_2 "../Alogrithm/config/2_ExcelSheetColumnTiTle.ini" #define FileName_3 "../Alogrithm/config/3_bool IsUgly.ini" #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini" +#define FileName_5 "../Alogrithm/config/5_MinDepth.ini" namespace UnitTest { @@ -73,4 +74,23 @@ namespace UnitTest } } }; + TEST_CLASS(UnitTest_5) + { + TEST_METHOD(TestMethode1) + { + char Section_Name[100][10] = { 0 }; + int Section_Count = CalcCount(100, Section_Name, FileName_5); + CString Na, nExpect; + for (int i = 0; i < Section_Count; i++) { + GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_5); + GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_5); + char return_str[100][50] = { 0 }; + int return_count = str_device2(Na, return_str); + TreeNode* root = CreatBitTree(return_str, return_count); + int nReal = MinDepth(root); + Assert::AreEqual(nReal,_ttoi(nExpect)); + free_tree(root); + } + } + }; } diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj b/Alogrithm/UnitTest/UnitTest.vcxproj index 0f62b5f..aa3f479 100644 --- a/Alogrithm/UnitTest/UnitTest.vcxproj +++ b/Alogrithm/UnitTest/UnitTest.vcxproj @@ -103,7 +103,7 @@ Windows $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) DebugFull - ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;%(AdditionalDependencies) + ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;%(AdditionalDependencies) diff --git a/Alogrithm/UnitTest/pch.cpp b/Alogrithm/UnitTest/pch.cpp index 3936926..39c05ed 100644 --- a/Alogrithm/UnitTest/pch.cpp +++ b/Alogrithm/UnitTest/pch.cpp @@ -70,7 +70,25 @@ int* str_device(CString str, int* value_count) return Section_devide; //返回数组首地址 } #endif - +//字符串切割函数,返回值为切割后元素的个数,每个元素的类型为字符串型 +int str_device2(CString str, char(*return_str)[50]) +{ + int value_count = 0; + char* token; //存放被切割后的第一个子串 + char Section_value[500] = { 0 };//存放nums转换成string类型的结果 + memset(Section_value, 0, sizeof(char) * 500); + strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串 + //获得切割到的第一个字符串 + token = strtok(Section_value, ","); + /* 继续获取其他的子字符串 */ + while (token != NULL) { + strcpy(return_str[value_count], token); + token = strtok(NULL, ","); + value_count++; //记录存了多少个元素 + } + return value_count; //返回数组首地址 +} +//字符串转bool bool CstrToBool(CString str) { if (str == "1") { @@ -78,3 +96,5 @@ bool CstrToBool(CString str) } return 0; } + + diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h index eb25a56..01f0539 100644 --- a/Alogrithm/UnitTest/pch.h +++ b/Alogrithm/UnitTest/pch.h @@ -17,11 +17,14 @@ #include"../Alogrithm/include/2_ExcelSheetColumnTitle.h" #include"../Alogrithm/include/3_bool IsUgly.h" #include"../Alogrithm/include/4_IsPalindrome.h" +#include"../Alogrithm/include/5_MinDepth.h" int CalcCount(int n, char(*str)[10],const char *FileName); int* str_device(CString str, int* value_count); +int str_device2(CString str, char(*return_str)[50]); bool CstrToBool(CString str); + #endif //PCH_H