@@ -156,6 +156,7 @@ | |||||
<ClCompile Include="src\21_Reverse.cpp" /> | <ClCompile Include="src\21_Reverse.cpp" /> | ||||
<ClCompile Include="src\22_Rotate.cpp" /> | <ClCompile Include="src\22_Rotate.cpp" /> | ||||
<ClCompile Include="src\23_RestoreIpAddresses.cpp" /> | <ClCompile Include="src\23_RestoreIpAddresses.cpp" /> | ||||
<ClCompile Include="src\24_LengthOfLongestSubstring.cpp" /> | |||||
<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" /> | ||||
@@ -182,6 +183,7 @@ | |||||
<ClInclude Include="include\21_Reverse.h" /> | <ClInclude Include="include\21_Reverse.h" /> | ||||
<ClInclude Include="include\22_Rotate.h" /> | <ClInclude Include="include\22_Rotate.h" /> | ||||
<ClInclude Include="include\23_RestoreIpAddresses.h" /> | <ClInclude Include="include\23_RestoreIpAddresses.h" /> | ||||
<ClInclude Include="include\24_LengthOfLongestSubstring.h" /> | |||||
<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" /> | ||||
@@ -207,6 +209,7 @@ | |||||
<None Include="config\21_Reverse.ini" /> | <None Include="config\21_Reverse.ini" /> | ||||
<None Include="config\22_Rotate.ini" /> | <None Include="config\22_Rotate.ini" /> | ||||
<None Include="config\23_RestoreIpAddresses.ini" /> | <None Include="config\23_RestoreIpAddresses.ini" /> | ||||
<None Include="config\24_LengthOfLongestSubstring.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" /> | ||||
@@ -96,6 +96,9 @@ | |||||
<ClCompile Include="src\23_RestoreIpAddresses.cpp"> | <ClCompile Include="src\23_RestoreIpAddresses.cpp"> | ||||
<Filter>源文件\src</Filter> | <Filter>源文件\src</Filter> | ||||
</ClCompile> | </ClCompile> | ||||
<ClCompile Include="src\24_LengthOfLongestSubstring.cpp"> | |||||
<Filter>源文件\src</Filter> | |||||
</ClCompile> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | <ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | ||||
@@ -167,6 +170,9 @@ | |||||
<ClInclude Include="include\23_RestoreIpAddresses.h"> | <ClInclude Include="include\23_RestoreIpAddresses.h"> | ||||
<Filter>头文件\include</Filter> | <Filter>头文件\include</Filter> | ||||
</ClInclude> | </ClInclude> | ||||
<ClInclude Include="include\24_LengthOfLongestSubstring.h"> | |||||
<Filter>头文件\include</Filter> | |||||
</ClInclude> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<None Include="config\3_bool IsUgly.ini"> | <None Include="config\3_bool IsUgly.ini"> | ||||
@@ -238,5 +244,8 @@ | |||||
<None Include="config\23_RestoreIpAddresses.ini"> | <None Include="config\23_RestoreIpAddresses.ini"> | ||||
<Filter>资源文件\config</Filter> | <Filter>资源文件\config</Filter> | ||||
</None> | </None> | ||||
<None Include="config\24_LengthOfLongestSubstring.ini"> | |||||
<Filter>资源文件\config</Filter> | |||||
</None> | |||||
</ItemGroup> | </ItemGroup> | ||||
</Project> | </Project> |
@@ -0,0 +1,30 @@ | |||||
[Test1] | |||||
Input=abcabcbb | |||||
Output=3 | |||||
[Test2] | |||||
Input=pwwkew | |||||
Output=3 | |||||
[Test3] | |||||
Input=aaaa | |||||
Output=1 | |||||
[Test4] | |||||
Input=aaaabbbbcccc | |||||
Output=2 | |||||
[Test5] | |||||
Input=asdferzzfa | |||||
Output=7 | |||||
[Test6] | |||||
Input=!@qqqwec,.pldfkfgooowf | |||||
Output=11 | |||||
[Test7] | |||||
Input=lyj18391569527@163.com | |||||
Output=12 | |||||
[Test8] | |||||
Input=www.baidu.com | |||||
Output=9 | |||||
[Test9] | |||||
Input=*****ABCabcbb***** | |||||
Output=7 | |||||
[Test10] | |||||
Input=()(@!,szadBNb) | |||||
Output=12 |
@@ -0,0 +1,3 @@ | |||||
#pragma once | |||||
#include <string.h> | |||||
int LengthOfLongestSubstring(char* s); |
@@ -0,0 +1,31 @@ | |||||
#include "../include/24_LengthOfLongestSubstring.h" | |||||
//题目:给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。 | |||||
//思路: 1.定义一个Index[128],每个字符的ASCII码在Index中对应一个int数,这个int数保存每个字符在字符串s出现的位置 | |||||
// 2.定义一个 int start,用来保存子串每次的初始位置 | |||||
// 3.挨个访问字符串s,当某个字符不是第一次出现且在子串起始位置之后,获取重复字符之间子串长度:i - start,和之前保存最长的子串比较,保存较长的那一个 | |||||
// 4.更新start到Index[s[i]],该重复字符上一次出现位置的下一个字符 | |||||
// 5.循环获取子串,求长度,比较得到长的字符串 | |||||
// 6.当最后一个字符找不到重复字符时,计算最后一个start到最后一个字符的长度 | |||||
int LengthOfLongestSubstring(char* s) { | |||||
int Index[128] = { 0 }; //每个字符的ASCII码作为数组的下标,Index用来存储每个字符在字符串s中的位置 | |||||
int len = 0; //存放子串的长度,每次遇到有重复字符时重新计算 | |||||
int max = 0; //存放最长子串,作为返回值 | |||||
int i = 0; //变量i,用来访问字符串s | |||||
int start = 0; //子串起始位置的下标,每次遇到重复子串时更新位置 | |||||
for ( i = 0; i < strlen(s); i++) { | |||||
if (Index[s[i]] > start) { //判断字符是不是第一次出现,且比起始位置要大 | |||||
len = i - start; //获得当前子串的长度 | |||||
if (len > max) { //如果当前子串比原来的子串长,则更新为当前子串 | |||||
max = len; | |||||
} | |||||
start = Index[s[i]]; //更新子串的起始位置:该重复字符上一次出现位置的下一个字符 | |||||
} | |||||
Index[s[i]] = i + 1; //把当前字符在字符串s中的位置放到Index中,如果字符多次出现就更新为最后出现的位置 | |||||
} | |||||
len = i - start; //如果遍历到最后一个字符,但是没找到重复的字符,则计算最后一个子串的长度 | |||||
return len > max ? len : max; | |||||
} | |||||
@@ -1,13 +1,9 @@ | |||||
#include<stdio.h> | #include<stdio.h> | ||||
#include "../include/23_RestoreIpAddresses.h" | |||||
#include"../include/24_LengthOfLongestSubstring.h" | |||||
int main() | int main() | ||||
{ | { | ||||
char str[] = "11112345"; | |||||
int m = 0; | |||||
char** res; | |||||
res = RestoreIpAddresses(str, &m); | |||||
for (int i = 0; i < m; i++) | |||||
printf("%s\n", res[i]); | |||||
char s[] = "lyj18391569527@163.com"; | |||||
printf("%d\n", LengthOfLongestSubstring(s)); | |||||
} | } |
@@ -25,6 +25,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; | |||||
#define FileName_21 "../Alogrithm/config/21_Reverse.ini" | #define FileName_21 "../Alogrithm/config/21_Reverse.ini" | ||||
#define FileName_22 "../Alogrithm/config/22_Rotate.ini" | #define FileName_22 "../Alogrithm/config/22_Rotate.ini" | ||||
#define FileName_23 "../Alogrithm/config/23_RestoreIpAddresses.ini" | #define FileName_23 "../Alogrithm/config/23_RestoreIpAddresses.ini" | ||||
#define FileName_24 "../Alogrithm/config/24_LengthOfLongestSubstring.ini" | |||||
@@ -462,4 +463,22 @@ namespace UnitTest | |||||
} | } | ||||
} | } | ||||
}; | }; | ||||
TEST_CLASS(UnitTest_24) | |||||
{ | |||||
TEST_METHOD(TestMethode1) | |||||
{ | |||||
char Section_Name[100][10] = { 0 }; | |||||
int Section_Count = CalcCount(100, Section_Name, FileName_24); | |||||
CString Input, Output; | |||||
char Str_char[1024] = { 0 }; | |||||
char* WordDic[100]; | |||||
for (int i = 0; i < Section_Count; i++) { | |||||
GetPrivateProfileString(Section_Name[i], "Input", " ", Input.GetBuffer(200), 200, FileName_24); | |||||
GetPrivateProfileString(Section_Name[i], "Output", " ", Output.GetBuffer(20), 20, FileName_24); | |||||
strcpy(Str_char, Input); | |||||
int nReal = LengthOfLongestSubstring(Str_char); | |||||
Assert::AreEqual(nReal, _ttoi(Output)); | |||||
} | |||||
} | |||||
}; | |||||
} | } |
@@ -36,7 +36,7 @@ | |||||
#include"../Alogrithm/include/21_Reverse.h" | #include"../Alogrithm/include/21_Reverse.h" | ||||
#include"../Alogrithm/include/22_Rotate.h" | #include"../Alogrithm/include/22_Rotate.h" | ||||
#include"../Alogrithm/include/23_RestoreIpAddresses.h" | #include"../Alogrithm/include/23_RestoreIpAddresses.h" | ||||
#include"../Alogrithm/include/24_LengthOfLongestSubstring.h" | |||||
int CalcCount(int n, char(*str)[10],const char *FileName); | int CalcCount(int n, char(*str)[10],const char *FileName); | ||||