leetcode3无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度。测试输入:’“abcabcbb”’,没有重复字符的最长字符串是‘“abc”’,那么长度就是3。输出:3#include <vector>#include <string>#include <iostream>#include <map>using namespace std;int...
·
给定一个字符串,找出不含有重复字符的最长子串的长度。
测试
输入:’“abcabcbb”’,没有重复字符的最长字符串是‘“abc”’,那么长度就是3。
输出:3
#include <vector>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int lengthOfLongestSubString(string s)
{
std::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]];
}
if((i-lastRepeatPos)<maxLen)
{
maxLen=i-lastRepeatPos;
}
m[s[i]]=i;
}
return maxLen;
}
更多推荐
已为社区贡献2条内容
所有评论(0)