想到的方法还是不错的,速度也不错。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int i,j,k;
int max=0;
int sleng=s.length();
int *count=new int[sleng];
count[sleng-1]=1;//最后一个子串必然为1长度
for(i=0;i<sleng-1;i++)
{
count[i]=1;
for(j=i+1;j<sleng;j++)
{
if(s[j]==s[i])//子串内有和串头相等的——我只找和串头相等的,比如abba,则第一个s[0]:a:count=3,s[1]b:count=1然后反查,更新s[0]count=2
{
count[i]=j-i;
for(k=i-1;k>=0;k--)
{
if(k+count[k]>i+count[i])
{
count[k]=i-k+count[i];
}
else
{
break;
}
}
break;
}
count[i]++;
}
}
for(i=0;i<sleng;i++)//遍历count找出最大值
{
if(count[i]>max)
{
max=count[i];
}
if(max>sleng-i)
{
break;
}
}
return max;
}
};