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.
int lengthOfLongestSubstring(string s) {
int hashtable[256];
memset(hashtable,-1,sizeof(hashtable));
int length=s.length(),cur=0,maxLength=0,curLength=0,index=0;
char ch=' ';
if(length==0) return 0;
if(length==1) return 1;
while (index<length)
{
cur=index;
while (cur<length)
{
ch=s.at(cur);
cur++;
if(hashtable[ch]==-1){
hashtable[ch]=0;
curLength++;
}
else if(hashtable[ch]==0){
if(maxLength<curLength){
maxLength=curLength;
}
curLength=0;
memset(hashtable,-1,sizeof(hashtable));
break;
}
}
index++;
}
return maxLength;
本文讨论了如何在给定字符串中找到无重复字符的最长子串,并提供了算法实现。
701

被折叠的 条评论
为什么被折叠?



