問題描述:
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;
}
};