topic:
Given a string, please find out the length of the longest substring that does not contain repeated characters .
Example:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是"abc",所以其长度为3
Original title address:
3. The longest substring without repeated characters
Problem-solving ideas:
Define two pointers left and right to move to the right respectively. When right points to a character, move left; otherwise, move right.
int lengthOfLongestSubstring(char * s){
int exist[256] = {0};
char *left = s;
char *right = s;
int max = 0;
while (*right != 0) {
if (exist[*right] == 1) {
exist[*left++] = 0; // 移除左侧字符,left右移
} else {
exist[*right++] = 1; // 记录该字符,right右移
int t = right - left;
if (t > max) max = t;
}
}
return max;
}