题解 | #最长不含重复字符的子字符串#
最长不含重复字符的子字符串
https://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7
#include <map>
#include <utility>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
int lengthOfLongestSubstring(string s) {
// write code here
map<char, bool> count;
int len;
int maxlen = 0;
for (int i = 0; i < s.size(); ++i) {
count.clear();
len = 0;
for (int j = i; j < s.size(); ++j) {
char c = s[j];
if (count[c] != true) { // 检查是否已经存在
count[c] = true; // 直接赋值为 true
++len;
if (len > maxlen) {
maxlen = len;
}
} else {
break;
}
}
}
return maxlen;
}
};


