/*****第一种方法的时间复杂度为O(n^2), 空间复杂度为O(1), 采用滑动窗口的方法,当遇到重复的字符时候,将左窗口滑动到第一次出现这个字符的后一位***/
int lengthOfLongestSubstring(char* s) {
int length = 0;
int maxLength = 0;
char* temp = s;
while((*temp) != '\0')
{
length++;
temp++;
}
temp = s;
char* repeater = s;
if(length == 0)
return 0;
if(length == 1)
return 1;
int k,i;
for(k = 0; k < length; k++)
{
i = (int)(temp - repeater);
int j = 0;
for(j = 0; j < i; j++)
{
if(*(repeater+j) == (*temp))
{
maxLength = maxLength > i ? maxLength : i;
repeater = repeater + j + 1;
i = 0;
break;
}
}
temp++;
}
maxLength = maxLength > (i+1) ? maxLength : (i+1);
return maxLength;
}
/****第二种也采用滑动窗口,不过一个好处是,他使用了一个地址表用于存放0-128的地址,方便以O(1)的时间复杂度查表,但牺牲了128个
地址空间复杂度,但因为只有128所以没关系****/
int lengthOfLongestSubstring(char* s)
{
int len=0;
char* end = s;
char* temp;
char* addressTable[128]={NULL};
while((*end) != '\0')
{
temp = addressTable[*end];
addressTable[*end] = end;
if(temp >= s)
{
len = (end-s) > len ? (end-s):len;
s = temp+1;
}
end++;
}
len=end-s>len?end-s:len;
return len;
}