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.
int lengthOfLongestSubstring(string s) {
int isExist[255];
memset(isExist, 0, 255*sizeof(int));
int cnt = 0, max_length = 0;
for (int i = 0, j = 0; i < s.length();){
if (!isExist[s[i]]){
++cnt;
++isExist[s[i]];
if (max_length < cnt)
max_length = cnt;
++i;
}
else{
--cnt;
--isExist[s[j]];
++j;
}
}
return max_length;
}
本文介绍了一种求解字符串中最长无重复字符子串长度的算法实现。通过使用数组记录字符出现状态,配合双指针技巧高效遍历字符串,找到符合条件的最长子串。
960

被折叠的 条评论
为什么被折叠?



