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.
此题相对来说就比较简单了,只要进行一个两层遍历,用一张表记录下字符是否出现过,然后用一个max变量记录当前获得的最大长度即可,代码如下
<span style="font-size:14px;">int lengthOfLongestSubstring(char* s) {
int i,j,p,max=0,l,b[500]={0};
l=strlen(s);
for(i=0;i<l;i++)
{
for(j=0;j<500;j++)
b[j]=0;
p=0;
for(j=i;j<l;j++)
{
if(b[(int)s[j]]==0)
{
p++;
b[(int)s[j]]=1;
}
else break;
}
if(p>max) max=p;
}
return max;
}</span>
考虑到因为时间复杂度为O(N^2)所以直接使用了C了进行解决,总共RE一次,WA一次,第三次AC,所花时间为48ms,在C相对较差了。