用滑动窗口来维护不重复的字符串,不重复则right指针+1,重复则left指针在重复的位置+1变不重复
#include<stdio.h>
#include<string.h>
int Max(int a,int b){
if(a>b) return a;
else return b;
}
int lengthOfLongestSubstring(char* s){
int len = strlen(s);
int max = 1;//统计最长不重复即可
int flag = 0;//用于记录是否出现重复字符
int left = 0;
int right = 0;
int i,j;
for(i = 0;i<len;i++){
if(left<=right){
flag = 0;
//j记录相同字母的下标,将其右移一位就不等了
for(j = left;j<right;j++){
if(s[j]==s[right]){
flag = 1;
break;
}
}
if(flag){
left = j+1;
}
}
max = Max(max,right-left+1);
right++;
}
return max;
}
int main(){
char s[] = "abcabcbb";
int length = lengthOfLongestSubstring(s);
printf("%d",length);
return 0;
}