LeedCode_ 无重复字符的最长子串

题目说明

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。

链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
分析:采用滑动窗口的思路

int lengthOfLongestSubstring(string s)
{
	unordered_set<char> lookup;
	int maxsize = 0;
	int n = s.size();
	if (n == 0) return 0;
	int left = 0;
	for (int i = 0; i < n; i++)
	{
		while(lookup.find(s[i]) != lookup.end())
		{
			lookup.erase(s[left]);
			left++;
		}
		lookup.insert(s[i]);
		maxsize = max(maxsize, i - left + 1);
	}
	return maxsize;
}
发布了63 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/luncy_yuan/article/details/104069976