Problem:
给定一个字符串s
,找出最长的不重复字符的子串的长度。
Solution:
int lengthOfLongestSubstring(char* fullString)
{
int head = 0, tail = 0, pos = 0, it = 0;
int currentLength = 0;
int longest = 0;
bool present = false;
while (fullString[head] != '\0')
{
pos = 0; present = false; //reset these guys on all iterations
for (it = tail; it < head; it++)
{
if(fullString[it] == fullString[head]) //checking for repetition from tail to just before head
{
present = true;
break;
}
}
pos = it; //get the position of repeated character
if (present)
{
longest = currentLength > longest ? currentLength : longest;
tail = pos + 1; //move tail to position just after duplicate was found
}
head ++;
currentLength = head - tail; //using current positions of head and tail, calculate length of substring
}
return longest > currentLength ? longest : currentLength;
}