您的位置:首页 > 其它

[LeetCode] 046: Longest Substring Without Repeating Characters

2017-09-10 20:49 260 查看
[Problem]
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.

[Solution]
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int res = 0, i = 0, j = 0;
map<char, int> lastIndex;
while(j < s.size()){
if(lastIndex.find(s[j]) != lastIndex.end() && lastIndex[s[j]] >= i){
i = lastIndex[s[j]] + 1;
}
res = max(res, j - i + 1);
lastIndex[s[j]] = j;
j++;
}
return res;
}
};
说明:版权所有,转载请注明出处。Coder007的博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: