/*given a string, find the length of the longest substring without repeating characters.
* For example, the longest substring without repeating letters for "abcabcbb" is "abc",
* which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
*/
#include <iostream>
#include <string>
#include <set>
class Solution
{
public:
int lengthOfLongestSubstring(std::string s)
{
if(s.size()==0)
return 0;
std::set<char> state;
int length=0;
auto begin=s.begin();
auto end=s.begin();
while(end!=s.end())
{
while(end!=s.end() && state.find(*end)==state.end())
{
state.insert(*end);
++end;
}
if(end-begin>length)
length=end-begin;
while(begin<=end && end!=s.end())
{
if(*begin==*end)
{
++begin;
++end;
break;
}
else
state.erase(*begin++);
}
}
return length;
}
};
int main(int argc,char* argv[])
{
Solution s;
std::string str("abcaadfeac");
std::cout<<s.lengthOfLongestSubstring(str)<<std::endl;
return 0;
}//程序貌似有误
最长非重复字串
最新推荐文章于 2022-02-22 23:21:41 发布