class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int i, j, k, len, preLen = 0, maxLen = 0;
map<char, int> m;
map<char, int> :: iterator it;
for(i = 0; i < s.size(); i++)
{
len = 0;
k = i + preLen - 1 < 0 ? 0 : i + preLen - 1;
for(j = k; j < s.size(); j++)
{
m[s[j]]++;
if(m[s[j]] > 1)
{
if(len == 0 && s[j] != s[i - 1])
preLen -= 1;
break;
}
if(j == k && j > 0)
len += preLen;
else if(j > k || j == 0)
len++;
}
maxLen = max(len, maxLen);
if(len)
preLen = len;
m[s[i]] = 0;
}
return maxLen;
}
};
leetcode:Longest Substring Without Repeating Characters
最新推荐文章于 2024-08-31 00:26:34 发布