diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo index 0f4c655..0d6b626 100644 Binary files a/Alogrithm/.vs/Alogrithm/v16/.suo and b/Alogrithm/.vs/Alogrithm/v16/.suo differ diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj b/Alogrithm/Alogrithm/Alogrithm.vcxproj index 7c3f8ef..7a3fa05 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj @@ -156,6 +156,7 @@ + @@ -182,6 +183,7 @@ + @@ -207,6 +209,7 @@ + diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters index 5ba3eae..08d2938 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters @@ -96,6 +96,9 @@ 源文件\src + + 源文件\src + @@ -167,6 +170,9 @@ 头文件\include + + 头文件\include + @@ -238,5 +244,8 @@ 资源文件\config + + 资源文件\config + \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/24_LengthOfLongestSubstring.ini b/Alogrithm/Alogrithm/config/24_LengthOfLongestSubstring.ini new file mode 100644 index 0000000..4a5ce4d --- /dev/null +++ b/Alogrithm/Alogrithm/config/24_LengthOfLongestSubstring.ini @@ -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 \ No newline at end of file diff --git a/Alogrithm/Alogrithm/include/24_LengthOfLongestSubstring.h b/Alogrithm/Alogrithm/include/24_LengthOfLongestSubstring.h new file mode 100644 index 0000000..5358553 --- /dev/null +++ b/Alogrithm/Alogrithm/include/24_LengthOfLongestSubstring.h @@ -0,0 +1,3 @@ +#pragma once +#include +int LengthOfLongestSubstring(char* s); \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/24_LengthOfLongestSubstring.cpp b/Alogrithm/Alogrithm/src/24_LengthOfLongestSubstring.cpp new file mode 100644 index 0000000..8c1ad93 --- /dev/null +++ b/Alogrithm/Alogrithm/src/24_LengthOfLongestSubstring.cpp @@ -0,0 +1,31 @@ +#include "../include/24_LengthOfLongestSubstring.h" +//ĿһַҳвظַӴijȡ +//˼·: 1.һIndex[128]ÿַASCIIIndexжӦһintintÿַַsֵλ +// 2.һ int startӴÿεijʼλ +// 3.ַsijַǵһγӴʼλ֮󣬻ȡظַ֮Ӵȣi - start֮ǰӴȽϣϳһ +// 4.startIndex[s[i]]ظַһγλõһַ +// 5.ѭȡӴ󳤶ȣȽϵõַ +// 6.һַҲظַʱһstartһַij +int LengthOfLongestSubstring(char* s) { + int Index[128] = { 0 }; //ÿַASCIIΪ±꣬Index洢ÿַַsеλ + int len = 0; //Ӵijȣÿظַʱ¼ + int max = 0; //ӴΪֵ + int i = 0; //iַs + int start = 0; //Ӵʼλõ±꣬ÿظӴʱλ + for ( i = 0; i < strlen(s); i++) { + if (Index[s[i]] > start) { //жַDzǵһγ֣ұʼλҪ + len = i - start; //õǰӴij + if (len > max) { //ǰӴԭӴΪǰӴ + max = len; + } + start = Index[s[i]]; //Ӵʼλ:ظַһγλõһַ + } + Index[s[i]] = i + 1; //ѵǰַַsеλ÷ŵIndexУַγ־͸Ϊֵλ + } + len = i - start; //һַûҵظַһӴij + return len > max ? len : max; +} + + + + diff --git a/Alogrithm/Alogrithm/src/main.cpp b/Alogrithm/Alogrithm/src/main.cpp index de59cee..dca5417 100644 --- a/Alogrithm/Alogrithm/src/main.cpp +++ b/Alogrithm/Alogrithm/src/main.cpp @@ -1,13 +1,9 @@ #include -#include "../include/23_RestoreIpAddresses.h" +#include"../include/24_LengthOfLongestSubstring.h" 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)); } diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp index 6e6d57e..f1acc43 100644 --- a/Alogrithm/UnitTest/UnitTest.cpp +++ b/Alogrithm/UnitTest/UnitTest.cpp @@ -25,6 +25,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #define FileName_21 "../Alogrithm/config/21_Reverse.ini" #define FileName_22 "../Alogrithm/config/22_Rotate.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)); + } + } + }; } diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h index 40fdd28..dbe2c0c 100644 --- a/Alogrithm/UnitTest/pch.h +++ b/Alogrithm/UnitTest/pch.h @@ -36,7 +36,7 @@ #include"../Alogrithm/include/21_Reverse.h" #include"../Alogrithm/include/22_Rotate.h" #include"../Alogrithm/include/23_RestoreIpAddresses.h" - +#include"../Alogrithm/include/24_LengthOfLongestSubstring.h" int CalcCount(int n, char(*str)[10],const char *FileName);