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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int nSize = s.size();
if (nSize <= 1) return nSize;
bool flag[256];
memset(flag, false, sizeof(bool)*256);
int i = 1;
int j = 0;
int longest = 1;
flag[s[0]] = true;
while(i < nSize)
{
if (flag[s[i]] == false)
{
flag[s[i]] = true;
++i;
}
else
{
while(s[j] != s[i])
{
flag[s[j]] = false;
++j;
}
++i;
++j;
}
longest = longest > i-j ? longest : i-j;
}
return longest;
}
};