计算一个字符串最大不重复子字符串的长度
//获取字符串最大不重复子字符串的长度
int lengthOfLongestSubstring(string s)
{
int result = 0;//保存上一次最大不重复子串长度
char c;
int i = 1;
int pos = 0;//保存出现重复的位置
bool isSame = false;//是否进行重复消除
vector<char> cList;//保存目前的不重复子串
//判断是否为空
if (!s.compare(""))
return 0;
cList.push_back(s[0]);
while (s[i] != '\0')
{
c = s[i];
for (int j = 0; j < cList.size(); j++) {
if (cList[j] == c) {
pos = j;
isSame = true;
break;
}
}
//如果出现相同,把第一个字符到出现重复位置的字符都删除,改变保存的子字符串
if (isSame)
{
int x = 0;
vector<char>::iterator it;
//保存目前出现过的最大子字符串长度
result = result>cList.size()?result:cList.size();
for (it = cList.begin();x<=pos; x++)
{
cList.erase(it);
}
isSame = false;
}
cList.push_back(c);
i++;
}
return result>cList.size()?result: cList.size();
}
该算法是用C++实现,需要包含头文件,使用其他语言实现类似