使用双指针,i遍历全部字符,start收集重复的次数,最后不重复出现的字符个数maxx为i-start+1;
// main.cpp
// Longest Substring
//
// Created by Bowie Hsu on 14/11/21.
// Copyright (c) 2014年 Bowie Hsu . All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int i,maxx=0,start=0;
bool sign[256]={false};
//int position[30];
memset(sign,0,sizeof(sign));
for(i=0;i<s.length();++i)
{
char c=s[i];
if (!sign[c]) { //第一次出现
sign[c]=true;//记录出现的位置
maxx=maxx>i-start+1?maxx:i-start+1;
}
else //重复,将start提前
{
//int j=start;
while(s[start] != c)
{
sign[s[start]] = false;
++start;
}
++start;
}
}
return maxx;
}
};
int main()
{
Solution x;
int ans;
ans=x.lengthOfLongestSubstring("wlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmco");
cout<<ans<<endl;
//cout<<"the result is"<<endl;
}