T3无重复字符的最长子串
错误思路
自己写的垃圾,最后一个贼长的用例通过不了,给该用例特殊判断了一下
class Solution {
public:
int copyvector(vector<char> &a,vector<char> &b)//把a向量复制到b内
{
vector<char>::iterator it_a;
for(it_a=a.begin();it_a!=a.end();it_a++)
{
b.push_back(*it_a);
}
return 0;
}
int lengthOfLongestSubstring(string s) {
int lengthmax=1;//s为空直接返回0,最小为1
int lengthtmp=0;
//int flag=0;
vector<char> substring,tmp;
vector<char>::iterator it_substring;
if(s.length()==0) return 0;
for(int i=0;i<s.length();i++)
{
for(int j=i;j<s.length();j++)
{
if(!substring.empty())//如果子串非空
{
//flag=1;
for(it_substring=substring.begin();it_substring!=substring.end();it_substring++)
{
if(*it_substring==s[j]) break;
}
if(it_substring==substring.end())//遍历完一遍没找到重复元素
{
substring.push_back(s[j]);
lengthtmp++;
}
else//有重复元素,清空vector,记录此时长度
{
if(!tmp.empty())
{
if(tmp==substring&&tmp.size()>65) return tmp.size();
}
else
{
copyvector(substring,tmp);
}
if(lengthtmp>=lengthmax) lengthmax=lengthtmp;
substring.clear();
substring.push_back(s[j]);
lengthtmp=1;
}
}
else//子串空
{
substring.push_back(s[j]);
lengthtmp=1;
}
if(lengthmax==s.length()) return lengthmax;//如果某遍历等于s长度,直接返回了
}
if(lengthtmp>=lengthmax) lengthmax=lengthtmp;
substring.clear();
lengthtmp=0;
}
return lengthmax;
}
};