输入:“pwwkew”
输出:“3”
过程:求最长不重复字符子串长度
int lengthOfLongestSubstring(string s)
{
string subStr = "";
int maxlen = 0;
for (int i = 0; i < s.length(); i++)
{
size_t found = subStr.find(s[i]);
if (subStr.find(s[i]) != string::npos) // found
{
subStr = subStr.substr(found+1);
}
subStr.push_back(s[i]);
maxlen = (maxlen < subStr.length()) ? subStr.length() : maxlen;
}
return maxlen;
}
int lengthOfLongestSubstring_star(string s) {
int start = 0;
int maxlen = 1;
if (s.size() == 0)
return 0;
vector<int> location(128, -1);//ascii 128 bit
for (int i = 0; i<s.length(); i++)
{
if (location[s[i]] >= start)
{
start = location[s[i]] + 1;
}
location[s[i]] = i;
int strlen = i - start + 1;
maxlen = max(maxlen, strlen);
}
return maxlen;
}