题目描述
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
解题思路
使用滑动窗口,然后使用unordered_set判断是否存在重复字符。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> map;
int end=0;
int ans=0;
for(int index=0; index<s.size(); ++index){
if(index!=0){
map.erase(s[index-1]);
}
while(end<s.size() && map.count(s[end])<1){
map.insert(s[end]);
end++;
}
ans = max(ans, end-index);
}
return ans;
}
};
该博客介绍了一种使用滑动窗口和unordered_set的数据结构来解决寻找字符串中不含有重复字符的最长子串长度的方法。通过移动窗口并利用哈希集检查字符是否重复,实现高效查找。
872

被折叠的 条评论
为什么被折叠?



