02太简单了,一遍过了就不贴了
03我竟然也看答案了,因为我把子串和子序列搞混了,day。
hash查表,队列删除。时间复杂度N,算法应该到了,但是不知道为啥时间只超过了57%的人
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.length();
unordered_set<char>a;
queue<char>b;
int maxx = 0;
for (int i = 0; i < n; ++i)
{
if (a.find(s[i]) == a.end()) ///<如果哈希中没有
{
b.push(s[i]);
a.insert(s[i]);
maxx = max(maxx, (int)a.size());
}
else ///<如果哈希中存在
{
while (b.front() != s[i])
{
a.erase(b.front());
b.pop();
}
b.pop();
b.push(s[i]);
}
}
return maxx;
}
};