剑指offer——48. 最长不含重复字符的子字符串

48. 最长不含重复字符的子字符串

题目描述

输入一个字符串(只包含 a~z 的字符),求其最长不含重复字符的子字符串的长度。例如对于 arabcacfr,最长不含重复字符的子字符串为 acfr,长度为 4。

解题思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int longestSubstringWithoutDuplication(string s) {
if(s.empty()) return 0;
int len = s.size();
int maxLen = 1;
set<int> tmp;
for(int i = 0; i < len; i ++)
{
tmp.clear();
for(int j = i - 1; j >= 0; j --)
{
//cout <<"i: "<< i << " j: " << j << " " << s[j] << " ";
if(s[j] == s[i] || !tmp.emplace(s[j] - '0').second) break;
// cout <<"i: "<< i << " j: " << j << " " << s[j] <<" " << tmp.size()<< " ";
}
maxLen = max(maxLen, (int)tmp.size() + 1);
//cout << " max: "<< maxLen << endl;
}
return maxLen;
}
};
创作不易,欢迎打赏!
-------------本文结束感谢您的阅读-------------