| @@ -30,7 +30,7 @@ | |||||
| <ConfigurationType>Application</ConfigurationType> | <ConfigurationType>Application</ConfigurationType> | ||||
| <UseDebugLibraries>true</UseDebugLibraries> | <UseDebugLibraries>true</UseDebugLibraries> | ||||
| <PlatformToolset>v142</PlatformToolset> | <PlatformToolset>v142</PlatformToolset> | ||||
| <CharacterSet>Unicode</CharacterSet> | |||||
| <CharacterSet>MultiByte</CharacterSet> | |||||
| </PropertyGroup> | </PropertyGroup> | ||||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||||
| <ConfigurationType>Application</ConfigurationType> | <ConfigurationType>Application</ConfigurationType> | ||||
| @@ -144,6 +144,7 @@ | |||||
| <ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | <ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | ||||
| <ClCompile Include="src\3_bool IsUgly.cpp" /> | <ClCompile Include="src\3_bool IsUgly.cpp" /> | ||||
| <ClCompile Include="src\4_IsPalindrome.cpp" /> | <ClCompile Include="src\4_IsPalindrome.cpp" /> | ||||
| <ClCompile Include="src\5_MinDepth.cpp" /> | |||||
| <ClCompile Include="src\main.cpp" /> | <ClCompile Include="src\main.cpp" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| @@ -151,12 +152,14 @@ | |||||
| <ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | <ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | ||||
| <ClInclude Include="include\3_bool IsUgly.h" /> | <ClInclude Include="include\3_bool IsUgly.h" /> | ||||
| <ClInclude Include="include\4_IsPalindrome.h" /> | <ClInclude Include="include\4_IsPalindrome.h" /> | ||||
| <ClInclude Include="include\5_MinDepth.h" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <None Include="config\1_ContainsNearbyDuplicate.ini" /> | <None Include="config\1_ContainsNearbyDuplicate.ini" /> | ||||
| <None Include="config\2_ExcelSheetColumnTiTle.ini" /> | <None Include="config\2_ExcelSheetColumnTiTle.ini" /> | ||||
| <None Include="config\3_bool IsUgly.ini" /> | <None Include="config\3_bool IsUgly.ini" /> | ||||
| <None Include="config\4_IsPalindrome.ini" /> | <None Include="config\4_IsPalindrome.ini" /> | ||||
| <None Include="config\5_MinDepth.ini" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||||
| <ImportGroup Label="ExtensionTargets"> | <ImportGroup Label="ExtensionTargets"> | ||||
| @@ -39,6 +39,9 @@ | |||||
| <ClCompile Include="src\4_IsPalindrome.cpp"> | <ClCompile Include="src\4_IsPalindrome.cpp"> | ||||
| <Filter>源文件\src</Filter> | <Filter>源文件\src</Filter> | ||||
| </ClCompile> | </ClCompile> | ||||
| <ClCompile Include="src\5_MinDepth.cpp"> | |||||
| <Filter>源文件\src</Filter> | |||||
| </ClCompile> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | <ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | ||||
| @@ -53,6 +56,9 @@ | |||||
| <ClInclude Include="include\4_IsPalindrome.h"> | <ClInclude Include="include\4_IsPalindrome.h"> | ||||
| <Filter>头文件\include</Filter> | <Filter>头文件\include</Filter> | ||||
| </ClInclude> | </ClInclude> | ||||
| <ClInclude Include="include\5_MinDepth.h"> | |||||
| <Filter>头文件\include</Filter> | |||||
| </ClInclude> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <None Include="config\3_bool IsUgly.ini"> | <None Include="config\3_bool IsUgly.ini"> | ||||
| @@ -67,5 +73,8 @@ | |||||
| <None Include="config\4_IsPalindrome.ini"> | <None Include="config\4_IsPalindrome.ini"> | ||||
| <Filter>资源文件\config</Filter> | <Filter>资源文件\config</Filter> | ||||
| </None> | </None> | ||||
| <None Include="config\5_MinDepth.ini"> | |||||
| <Filter>资源文件\config</Filter> | |||||
| </None> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| </Project> | </Project> | ||||
| @@ -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 | |||||
| @@ -0,0 +1,18 @@ | |||||
| #pragma once | |||||
| #include <stdio.h> | |||||
| #include <math.h> | |||||
| #include <atlstr.h> | |||||
| 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); | |||||
| @@ -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; | |||||
| } | |||||
| } | |||||
| @@ -6,6 +6,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; | |||||
| #define FileName_2 "../Alogrithm/config/2_ExcelSheetColumnTiTle.ini" | #define FileName_2 "../Alogrithm/config/2_ExcelSheetColumnTiTle.ini" | ||||
| #define FileName_3 "../Alogrithm/config/3_bool IsUgly.ini" | #define FileName_3 "../Alogrithm/config/3_bool IsUgly.ini" | ||||
| #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini" | #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini" | ||||
| #define FileName_5 "../Alogrithm/config/5_MinDepth.ini" | |||||
| namespace UnitTest | 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); | |||||
| } | |||||
| } | |||||
| }; | |||||
| } | } | ||||
| @@ -103,7 +103,7 @@ | |||||
| <SubSystem>Windows</SubSystem> | <SubSystem>Windows</SubSystem> | ||||
| <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||||
| <GenerateDebugInformation>DebugFull</GenerateDebugInformation> | <GenerateDebugInformation>DebugFull</GenerateDebugInformation> | ||||
| <AdditionalDependencies>../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;%(AdditionalDependencies)</AdditionalDependencies> | |||||
| <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)</AdditionalDependencies> | |||||
| </Link> | </Link> | ||||
| </ItemDefinitionGroup> | </ItemDefinitionGroup> | ||||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||
| @@ -70,7 +70,25 @@ int* str_device(CString str, int* value_count) | |||||
| return Section_devide; //返回数组首地址 | return Section_devide; //返回数组首地址 | ||||
| } | } | ||||
| #endif | #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) | bool CstrToBool(CString str) | ||||
| { | { | ||||
| if (str == "1") { | if (str == "1") { | ||||
| @@ -78,3 +96,5 @@ bool CstrToBool(CString str) | |||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| @@ -17,11 +17,14 @@ | |||||
| #include"../Alogrithm/include/2_ExcelSheetColumnTitle.h" | #include"../Alogrithm/include/2_ExcelSheetColumnTitle.h" | ||||
| #include"../Alogrithm/include/3_bool IsUgly.h" | #include"../Alogrithm/include/3_bool IsUgly.h" | ||||
| #include"../Alogrithm/include/4_IsPalindrome.h" | #include"../Alogrithm/include/4_IsPalindrome.h" | ||||
| #include"../Alogrithm/include/5_MinDepth.h" | |||||
| int CalcCount(int n, char(*str)[10],const char *FileName); | int CalcCount(int n, char(*str)[10],const char *FileName); | ||||
| int* str_device(CString str, int* value_count); | int* str_device(CString str, int* value_count); | ||||
| int str_device2(CString str, char(*return_str)[50]); | |||||
| bool CstrToBool(CString str); | bool CstrToBool(CString str); | ||||
| #endif //PCH_H | #endif //PCH_H | ||||