LeetCode 3 Longest Substring Without Repeating Characters,leetcoderepeating
分享于 点击 44201 次 点评:281
LeetCode 3 Longest Substring Without Repeating Characters,leetcoderepeating
LeetCode
3 Longest Substring Without Repeating Characters
题目
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)
{
map<char, int> m;
int maxLen = 0;
int lastRepeatPos = -1;
for(int i = 0; i < s.size(); i++)
{
if (m.find(s[i]) != m.end() && lastRepeatPos < m[s[i]])
lastRepeatPos = m[s[i]];
else if ( i - lastRepeatPos > maxLen )
maxLen = i - lastRepeatPos;
m[s[i]] = i;
}
return maxLen;
}
};
相关文章
- 暂无相关文章
用户点评