问题描述:
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.
问题分析:
找到一个字符串中不含有重复字符的最长的子串。
示例代码:
以下算法不正确
class Solution {
public:
int lengthOfLongestSubstring(string s)
{
int n, maxVal;
n = s.length();
maxVal = n == 0 ? 0 : 1;
for (int i = 1; i < n; i++)
{
int cur = 1;
for (int j = i - 1; j >= 0 && s[j] != s[i]; cur++, j--);
if (cur > maxVal)
{
maxVal = cur;
}
}
return maxVal;
}
};