c语言:
int lengthOfLongestSubstring(char* s) {
int index = 0;
int max = 1;
int i=0;
while(s[i]!=NULL)
{
i++;
}
int len = i;
if( len == 0)
return 0;
if (len ==1)
return 1;
for (int i =1; i< len; i++)
{
for(int j=i-1; j>=index; j--)
{
if(s[i] == s[j])
{
index = j+1;
break;
}
else
{
if(max < i-j+1)
max = i - j +1;
}
}
}
return max;
}
c++:
#define REC(i,s,n) for(int i=(s);i<n;i++)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int begin=0;
int result=0;
string word="";
int char_map[128]={0};
REC(i, 0,s.length()){
char_map[s[i]]++;
if(char_map[s[i]]==1){
word+=s[i];
if(result<word.length())
result=word.length();
}
else{
while(begin<i&&char_map[s[i]]>1){
char_map[s[begin]]--;
begin++;
}
word="";
for(int j=begin;j<=i;j++){
word+=s[i];
}
}
}
return result;
}
};