LeetCode Longest Substring Without Repeating Characters,leetcoderepeating
分享于 点击 25068 次 点评:117
LeetCode Longest Substring Without Repeating Characters,leetcoderepeating
Longest Substring Without Repeating CharactersGiven 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
map<char,int> mymap;
vector<int> pos;
for(int i=0;i<s.size();++i){
pos.push_back(mymap[s[i]]);
mymap[s[i]]=i+1;
}
int result=0,curr=0;
for(int j=0;j<s.size();++j){
curr=max(curr,pos[j]);
result=max(result,j-curr+1);
}
return result;
}
};
相关文章
- 暂无相关文章
用户点评