【leetcode】3. 无重复字符的最长子串
程序员文章站
2024-02-25 09:45:34
...
int lengthOfLongestSubstring(char* s) {
int templength,length = 0;
int i,j,k,flag = 0;
if(s[0] == '\0')return 0;//1.空
for(i = 0;s[i+1] != '\0';i++){//2.有重复
for(j = i+1;s[j] != '\0';j++){
for(k = i;k < j;k++){
if(s[k] == s[j]){
templength = j-i;
flag = 1;//重复截至会break
if(templength > length)length = templength;
break;
}
if(k == j-1){//非重复截至不break
templength = j-i+1;
if(templength > length)length = templength;
}
}
if(flag){
flag = 0;break;
}
}
}
if(length == 0){//3.无重复
length = i + 1;
}
return length;
}
一个实际表现非常糟糕的代码,毫无算法可言,,有时间再重写吧