03.无重复字符的最长子串

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;
}
};
发布了21 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qigezuishuaide/article/details/101173742
今日推荐